{"id":1711,"date":"2016-03-28T19:16:22","date_gmt":"2016-03-28T11:16:22","guid":{"rendered":"http:\/\/www.51cos.com\/?p=1711"},"modified":"2016-03-28T19:26:27","modified_gmt":"2016-03-28T11:26:27","slug":"linux%e7%b3%bb%e7%bb%9f%e4%b8%8b%e7%94%b5%e6%b1%a0%e9%a9%b1%e5%8a%a8%e7%a0%94%e7%a9%b6","status":"publish","type":"post","link":"http:\/\/www.51cos.com\/?p=1711","title":{"rendered":"Linux\u7cfb\u7edf\u4e0b\u7535\u6c60\u9a71\u52a8\u7814\u7a76"},"content":{"rendered":"<p>Linux\u7cfb\u7edf\u4e0b\u7535\u6c60\u9a71\u52a8\u4e00\u822c\u6709\u4e24\u79cd\u65b9\u5f0f\uff1a<\/p>\n<p>1.\u5916\u6302PMU\u82af\u7247\uff0c\u901a\u8fc7I2C\u63a5\u53e3\u8bfb\u53d6\u76f8\u5e94\u7684\u6570\u636e\uff1b<\/p>\n<p>2.\u901a\u8fc7\u82af\u7247 ADC\u63a5\u53e3\u83b7\u53d6\u7535\u91cf\uff1b<\/p>\n<p>\u672c\u6587\u8c08\u7684\u662f\u7b2c\u4e8c\u70b9\u3002<\/p>\n<p>\u5f53\u6211\u4eec\u8981\u5199\u4e00\u4e2a\u9502\u7535\u6c60\u7684\u9a71\u52a8\u7a0b\u5e8f\u7684\u65f6\u5019\uff0c\u9996\u5148\u8981\u77e5\u9053\u5c31\u662f\u5f53\u9a71\u52a8\u6302\u8f7d\u5230\u5185\u6838\u4e0a\u7684\u65f6\u5019\uff0c\u5185\u6838\u662f\u600e\u4e48\u77e5\u9053\u9a71\u52a8\u4e2d\u7684\u4fe1\u606f\u7684\uff0c\u5982\u4f55\u6765\u63a7\u5236\u9a71\u52a8\u3002\u5185\u6838\u63d0\u4f9b\u7ed9\u7535\u6e90\u9a71\u52a8\u7684\u63a5\u53e3\u5c31\u662f\u4e00\u4e2a\u7ed3\u6784\u4f53power_supply,struct power_supply \u5728\u6587\u4ef6 include\\linux\\power_supply.h \u4e2d\uff1a<\/p>\n<pre class=\"prettyprint linenums\">\nstruct power_supply {<br \/>\n\tconst char *name;<br \/>\n\tenum power_supply_type type;<br \/>\n\tenum power_supply_property *properties;\/\/\u58f0\u660e\u4e86\u7535\u6e90\u7684\u5c5e\u6027<br \/>\n\tsize_t num_properties;<\/p>\n<p>\tchar **supplied_to;<br \/>\n\tsize_t num_supplicants;<\/p>\n<p>\tint (*get_property)(struct power_supply *psy,<br \/>\n\t\t\t    enum power_supply_property psp,<br \/>\n\t\t\t    union power_supply_propval *val);<br \/>\n\tvoid (*external_power_changed)(struct power_supply *psy);<\/p>\n<p>\t\/* For APM emulation, think legacy userspace. *\/<br \/>\n\tint use_for_apm;<\/p>\n<p>\t\/* private *\/<br \/>\n\tstruct device *dev;<br \/>\n\tstruct work_struct changed_work;<\/p>\n<p>#ifdef CONFIG_LEDS_TRIGGERS<br \/>\n\tstruct led_trigger *charging_full_trig;<br \/>\n\tchar *charging_full_trig_name;<br \/>\n\tstruct led_trigger *charging_trig;<br \/>\n\tchar *charging_trig_name;<br \/>\n\tstruct led_trigger *full_trig;<br \/>\n\tchar *full_trig_name;<br \/>\n\tstruct led_trigger *online_trig;<br \/>\n\tchar *online_trig_name;<br \/>\n#endif<br \/>\n};<\/p>\n<p>\u6bcf\u79cd\u5c5e\u6027\u90fd\u6709\u7279\u6b8a\u7684\u542b\u4e49\u548c\u5355\u4f4d\uff0c\u7531\u4e8e\u8fd9\u4e9b\u5c5e\u6027\u666e\u904d\u9002\u7528\u4e8e\u5404\u79cd\u7535\u6e90\uff0c\u4f46\u662f\u5b9e\u9645\u5e94\u7528\u4e2d\u6709\u4e9b\u7535\u6e90\u53ef\u80fd\u65e0\u6cd5\u63d0\u4f9b\u67d0\u4e9b\u5c5e\u6027\uff0c\u6240\u4ee5\u8fd9\u4e9b\u5c5e\u6027\u9a71\u52a8\u53ef\u4ee5\u4e0d\u63d0\u4f9b\u3002<br \/>\n\u7535\u6e90\u7c7b\u662f\u53ef\u6269\u5c55\u7684\uff0c\u5141\u8bb8\u9a71\u52a8\u7a0b\u5e8f\u5b9a\u4e49\u5176\u81ea\u8eab\u9700\u8981\u7684\u5c5e\u6027\uff0c\u629b\u5f03\u4e0d\u9700\u8981\u7684\u5c5e\u6027\u3002<br \/>\n\u5b83\u8fd8\u96c6\u6210\u4e86LED\u6846\u67b6\uff0c\u7528\u6765\u8868\u793a\u7535\u6c60\u5145\u7535\/\u5b8c\u5168\u5145\u7535\u72b6\u6001\u548cAC \/ USB\u7535\u6e90\u7ebf\u4e0a\u72b6\u6001\u3002<\/p>\n<p>\u4e00\u822c\u6211\u4eec\u9700\u8981\u586b\u5145\u7684\u662f\u8fd9\u51e0\u4e2a\uff1a<\/p>\n<pre class=\"prettyprint linenums\" >\nstruct power_supply bat_am7x = {<br \/>\n\t.name\t\t\t= \"am7x-battery\", \/\/\u540d\u79f0 \u5bf9\u5e94\u4e8e\/sys\/class\/power_supply\/xxx\u6587\u4ef6\u5939<br \/>\n\t.type\t\t\t= POWER_SUPPLY_TYPE_BATTERY,\/\/\u7c7b\u578b\uff1a\u7535\u6c60<br \/>\n\t.properties\t\t= am7x_bat_main_props,\/\/\u5c5e\u6027\u7684\u7c7b\u522b\uff0c\u5982\u7535\u6c60\u72b6\u6001\u3001\u7535\u91cf\u3001\u6e29\u5ea6<br \/>\n\t.num_properties\t\t= ARRAY_SIZE(am7x_bat_main_props),\/\/\u5c5e\u6027\u7684\u6570\u91cf<br \/>\n\t.get_property\t\t= am7x_bat_get_property,\/\/\u83b7\u53d6\u5c5e\u6027\u7684\u56de\u8c03\u51fd\u6570<br \/>\n\t.use_for_apm\t\t= 1,<br \/>\n};<\/p>\n<p>err = power_supply_register(&pdev->dev,am7x_battery_info->bat);\/\/\u5c06\u7535\u6c60\u6ce8\u518c power_supply_register\u8c03\u7528\u5185\u6838\u63d0\u4f9b\u7684\u51fd\u6570device_create\u548cpower_supply_create_attrs\u6765\u5b9e\u73b0\u7535\u6c60\u7684\u6ce8\u518c\u3002 \u6ce8\u518c\u5b8c\u6210\u540e\u751f\u6210sys\/class\/power_supply\/\u7535\u6c60\u9a71\u52a8\u540d\u79f0\uff0c\u51fd\u6570\u5728drivers\\power\\power_supply_core.c \u4e2d<\/p>\n<p>\u4f7f\u7528\u65b9\u6cd5\u4e3e\u4f8b\uff1a<\/p>\n<pre class=\"prettyprint linenums\" >\n# cd \/sys\/class\/power_supply\/\u4f60\u7684\u7535\u6c60battery\/<\/p>\n<p># cat type status health present technology capacity<\/p>\n<p>Battery<\/p>\n<p>Charging<\/p>\n<p>Good<\/p>\n<p>1<\/p>\n<p>Li-ion<\/p>\n<p>50<\/p>\n<p>\u8fd9\u4e9b\u5b8f\u5df2\u7ecf\u5b9a\u4e49\u8fc7:<\/p>\n<pre class=\"prettyprint linenums\" >\n<p>enum { \/\/ \u5bf9\u5e94 POWER_SUPPLY_PROP_STATUS<\/p>\n<p>       POWER_SUPPLY_STATUS_UNKNOWN = 0,<\/p>\n<p>       POWER_SUPPLY_STATUS_CHARGING,<\/p>\n<p>       POWER_SUPPLY_STATUS_DISCHARGING,<\/p>\n<p>       POWER_SUPPLY_STATUS_NOT_CHARGING,<\/p>\n<p>       POWER_SUPPLY_STATUS_FULL,<\/p>\n<p>};<\/p>\n<p>enum {   \/\/ \u5bf9\u5e94 POWER_SUPPLY_PROP_CHARGE_TYPE<\/p>\n<p>       POWER_SUPPLY_CHARGE_TYPE_UNKNOWN = 0,<\/p>\n<p>       POWER_SUPPLY_CHARGE_TYPE_NONE,<\/p>\n<p>       POWER_SUPPLY_CHARGE_TYPE_TRICKLE,<\/p>\n<p>       POWER_SUPPLY_CHARGE_TYPE_FAST,<\/p>\n<p>};<\/p>\n<p>enum { \/\/ \u5bf9\u5e94 POWER_SUPPLY_PROP_HEALTH<\/p>\n<p>       POWER_SUPPLY_HEALTH_UNKNOWN = 0,<\/p>\n<p>       POWER_SUPPLY_HEALTH_GOOD,<\/p>\n<p>       POWER_SUPPLY_HEALTH_OVERHEAT,<\/p>\n<p>       POWER_SUPPLY_HEALTH_DEAD,<\/p>\n<p>       POWER_SUPPLY_HEALTH_OVERVOLTAGE,<\/p>\n<p>       POWER_SUPPLY_HEALTH_UNSPEC_FAILURE,<\/p>\n<p>       POWER_SUPPLY_HEALTH_COLD,<\/p>\n<p>};<\/p>\n<p>enum {  \/\/ \u5bf9\u5e94 POWER_SUPPLY_PROP_TECHNOLOGY<\/p>\n<p>       POWER_SUPPLY_TECHNOLOGY_UNKNOWN = 0,<\/p>\n<p>       POWER_SUPPLY_TECHNOLOGY_NiMH,<\/p>\n<p>       POWER_SUPPLY_TECHNOLOGY_LION,<\/p>\n<p>       POWER_SUPPLY_TECHNOLOGY_LIPO,<\/p>\n<p>       POWER_SUPPLY_TECHNOLOGY_LiFe,<\/p>\n<p>       POWER_SUPPLY_TECHNOLOGY_NiCd,<\/p>\n<p>       POWER_SUPPLY_TECHNOLOGY_LiMn,<\/p>\n<p>};<\/p>\n<p>enum { \/\/ \u5bf9\u5e94 POWER_SUPPLY_PROP_CAPACITY_LEVEL <\/p>\n<p>       POWER_SUPPLY_CAPACITY_LEVEL_UNKNOWN = 0,<\/p>\n<p>       POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL,<\/p>\n<p>       POWER_SUPPLY_CAPACITY_LEVEL_LOW,<\/p>\n<p>       POWER_SUPPLY_CAPACITY_LEVEL_NORMAL,<\/p>\n<p>       POWER_SUPPLY_CAPACITY_LEVEL_HIGH,<\/p>\n<p>       POWER_SUPPLY_CAPACITY_LEVEL_FULL,<\/p>\n<p>};<\/p>\n<p>\u7535\u6e90\u9a71\u52a8\u4e00\u822c\u9700\u8981\u505a\u4ee5\u4e0b\u51e0\u4e2a\u90e8\u5206\u7684\u5de5\u4f5c\uff1a<br \/>\n1\u3001\t\u5b9a\u4e49struct power_supply\uff0c\u8be5\u5b9a\u4e49\u53ef\u4ee5\u662f\u5168\u5c40\u7684\u6216\u8005\u5d4c\u5165\u5230\u9a71\u52a8\u4e13\u6709\u6570\u636e\u7ed3\u6784\u4e2d\uff0c\u5b9e\u73b0\u5176\u7ed3\u6784\u5b57\u6bb5<br \/>\n\uff081\uff09\tconst char *name;\u5b57\u6bb5<br \/>\n\u540d\u79f0<br \/>\n\uff082\uff09\tenum power_supply_type type;\u7535\u6e90\u7c7b\u578b\u5b57\u6bb5<br \/>\n\uff083\uff09\tenum power_supply_property *properties;\u5c5e\u6027\u5b57\u6bb5<br \/>\n\u5b9a\u4e49\u4e00\u4e2a\u5168\u5c40enum power_supply_property\u7c7b\u578b\u7684\u6570\u7ec4\uff0c\u6570\u7ec4\u4e2d\u4fdd\u5b58\u8be5\u7535\u6e90\u9700\u8981\u4e0a\u62a5\u7684\u5c5e\u6027\u3002\u5c06\u8be5\u6570\u7ec4\u7684\u9996\u5730\u5740\u8d4b\u7ed9properties\u5b57\u6bb5\u3002<br \/>\n\uff084\uff09\t\u5b9e\u73b0get_property\u51fd\u6570\uff0c\u7528\u6765\u8bfb\u53d6\u5c5e\u6027\u503c<br \/>\n\uff085\uff09\t\u5982\u679c\u9700\u8981\u5199\u5c5e\u6027\u503c\uff0c\u5219\u5b9e\u73b0set_property\u51fd\u6570\u548cproperty_is_writeable\u51fd\u6570\uff0c property_is_writeable\u51fd\u6570\u5224\u65ad\u662f\u5426\u9700\u8981\u5199\u6743\u9650\uff0c\u662f\u5219\u8fd4\u56de1\uff0c\u5426\u5219\u8fd4\u56de0\uff1b<br \/>\n2\u3001\t\u8c03\u7528\u51fd\u6570power_supply_register\u5c06\u5b9a\u4e49\u7684power_supply\u6ce8\u518c\u5230\u5185\u6838\u4e2d<br \/>\n3\u3001\t\u5f53\u7535\u6e90\u7684\u53c2\u6570\u6709\u53d8\u5316\u65f6\uff0c\u8c03\u7528power_supply_changed\u51fd\u6570\u901a\u77e5\u7cfb\u7edf\u548c\u7528\u6237\uff0c\u6539\u53d8\u76f8\u5e94\u7684\u5c5e\u6027\u503c\uff0c\u7136\u540e\u7528\u6237\u7a0b\u5e8f\u901a\u8fc7\/sys\/class\/power_supply\/\u4f60\u7684\u7535\u6c60\u540d\u79f0\/\u5c31\u53ef\u4ee5\u77e5\u9053\u5c5e\u6027\u503c\u3002<\/p>\n<p>\u53c2\u8003\u8d44\u6599\uff1a<\/p>\n<p><a href=\"http:\/\/www.51cos.com\/wp-content\/uploads\/2016\/03\/Linux\u7535\u6e90\u7ba1\u7406\u7cfb\u7edf\u67b6\u6784\u548c\u9a71\u52a8.pdf\"rel=\"\">Linux\u7535\u6e90\u7ba1\u7406\u7cfb\u7edf\u67b6\u6784\u548c\u9a71\u52a8<\/a><\/p>\n<p><a href=\"http:\/\/www.51cos.com\/wp-content\/uploads\/2016\/03\/linux\u7535\u6e90\u9a71\u52a8\u89e3\u6790.docx\"rel=\"\">linux\u7535\u6e90\u9a71\u52a8\u89e3\u6790<\/a><\/p>\n<p>http:\/\/www.wowotech.net\/pm_architecture.html<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Linux\u7cfb\u7edf\u4e0b\u7535\u6c60\u9a71\u52a8\u4e00\u822c\u6709\u4e24\u79cd\u65b9\u5f0f\uff1a 1.\u5916\u6302PMU\u82af\u7247\uff0c\u901a\u8fc7I2C\u63a5\u53e3\u8bfb\u53d6\u76f8\u5e94\u7684\u6570\u636e\uff1b 2.\u901a\u8fc7\u82af\u7247 A [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-1711","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"http:\/\/www.51cos.com\/index.php?rest_route=\/wp\/v2\/posts\/1711","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.51cos.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.51cos.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.51cos.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.51cos.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1711"}],"version-history":[{"count":5,"href":"http:\/\/www.51cos.com\/index.php?rest_route=\/wp\/v2\/posts\/1711\/revisions"}],"predecessor-version":[{"id":1718,"href":"http:\/\/www.51cos.com\/index.php?rest_route=\/wp\/v2\/posts\/1711\/revisions\/1718"}],"wp:attachment":[{"href":"http:\/\/www.51cos.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1711"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.51cos.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1711"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.51cos.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1711"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}