i2c-core.c 94 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519
  1. /* i2c-core.c - a device driver for the iic-bus interface */
  2. /* ------------------------------------------------------------------------- */
  3. /* Copyright (C) 1995-99 Simon G. Vogl
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details. */
  12. /* ------------------------------------------------------------------------- */
  13. /* With some changes from Kyösti Mälkki <kmalkki@cc.hut.fi>.
  14. All SMBus-related things are written by Frodo Looijaard <frodol@dds.nl>
  15. SMBus 2.0 support by Mark Studebaker <mdsxyz123@yahoo.com> and
  16. Jean Delvare <jdelvare@suse.de>
  17. Mux support by Rodolfo Giometti <giometti@enneenne.com> and
  18. Michael Lawnick <michael.lawnick.ext@nsn.com>
  19. OF support is copyright (c) 2008 Jochen Friedrich <jochen@scram.de>
  20. (based on a previous patch from Jon Smirl <jonsmirl@gmail.com>) and
  21. (c) 2013 Wolfram Sang <wsa@the-dreams.de>
  22. I2C ACPI code Copyright (C) 2014 Intel Corp
  23. Author: Lan Tianyu <tianyu.lan@intel.com>
  24. I2C slave support (c) 2014 by Wolfram Sang <wsa@sang-engineering.com>
  25. */
  26. #define pr_fmt(fmt) "i2c-core: " fmt
  27. #include <dt-bindings/i2c/i2c.h>
  28. #include <asm/uaccess.h>
  29. #include <linux/acpi.h>
  30. #include <linux/clk/clk-conf.h>
  31. #include <linux/completion.h>
  32. #include <linux/delay.h>
  33. #include <linux/err.h>
  34. #include <linux/errno.h>
  35. #include <linux/gpio.h>
  36. #include <linux/hardirq.h>
  37. #include <linux/i2c.h>
  38. #include <linux/idr.h>
  39. #include <linux/init.h>
  40. #include <linux/irqflags.h>
  41. #include <linux/jump_label.h>
  42. #include <linux/kernel.h>
  43. #include <linux/module.h>
  44. #include <linux/mutex.h>
  45. #include <linux/of_device.h>
  46. #include <linux/of.h>
  47. #include <linux/of_irq.h>
  48. #include <linux/pm_domain.h>
  49. #include <linux/pm_runtime.h>
  50. #include <linux/pm_wakeirq.h>
  51. #include <linux/property.h>
  52. #include <linux/rwsem.h>
  53. #include <linux/slab.h>
  54. #include "i2c-core.h"
  55. #define CREATE_TRACE_POINTS
  56. #include <trace/events/i2c.h>
  57. #define I2C_ADDR_OFFSET_TEN_BIT 0xa000
  58. #define I2C_ADDR_OFFSET_SLAVE 0x1000
  59. /* core_lock protects i2c_adapter_idr, and guarantees
  60. that device detection, deletion of detected devices, and attach_adapter
  61. calls are serialized */
  62. static DEFINE_MUTEX(core_lock);
  63. static DEFINE_IDR(i2c_adapter_idr);
  64. static struct device_type i2c_client_type;
  65. static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver);
  66. static struct static_key i2c_trace_msg = STATIC_KEY_INIT_FALSE;
  67. static bool is_registered;
  68. void i2c_transfer_trace_reg(void)
  69. {
  70. static_key_slow_inc(&i2c_trace_msg);
  71. }
  72. void i2c_transfer_trace_unreg(void)
  73. {
  74. static_key_slow_dec(&i2c_trace_msg);
  75. }
  76. #if defined(CONFIG_ACPI)
  77. struct i2c_acpi_handler_data {
  78. struct acpi_connection_info info;
  79. struct i2c_adapter *adapter;
  80. };
  81. struct gsb_buffer {
  82. u8 status;
  83. u8 len;
  84. union {
  85. u16 wdata;
  86. u8 bdata;
  87. u8 data[0];
  88. };
  89. } __packed;
  90. struct i2c_acpi_lookup {
  91. struct i2c_board_info *info;
  92. acpi_handle adapter_handle;
  93. acpi_handle device_handle;
  94. acpi_handle search_handle;
  95. u32 speed;
  96. u32 min_speed;
  97. };
  98. static int i2c_acpi_fill_info(struct acpi_resource *ares, void *data)
  99. {
  100. struct i2c_acpi_lookup *lookup = data;
  101. struct i2c_board_info *info = lookup->info;
  102. struct acpi_resource_i2c_serialbus *sb;
  103. acpi_status status;
  104. if (info->addr || ares->type != ACPI_RESOURCE_TYPE_SERIAL_BUS)
  105. return 1;
  106. sb = &ares->data.i2c_serial_bus;
  107. if (sb->type != ACPI_RESOURCE_SERIAL_TYPE_I2C)
  108. return 1;
  109. status = acpi_get_handle(lookup->device_handle,
  110. sb->resource_source.string_ptr,
  111. &lookup->adapter_handle);
  112. if (!ACPI_SUCCESS(status))
  113. return 1;
  114. info->addr = sb->slave_address;
  115. lookup->speed = sb->connection_speed;
  116. if (sb->access_mode == ACPI_I2C_10BIT_MODE)
  117. info->flags |= I2C_CLIENT_TEN;
  118. return 1;
  119. }
  120. static int i2c_acpi_do_lookup(struct acpi_device *adev,
  121. struct i2c_acpi_lookup *lookup)
  122. {
  123. struct i2c_board_info *info = lookup->info;
  124. struct list_head resource_list;
  125. int ret;
  126. if (acpi_bus_get_status(adev) || !adev->status.present ||
  127. acpi_device_enumerated(adev))
  128. return -EINVAL;
  129. memset(info, 0, sizeof(*info));
  130. lookup->device_handle = acpi_device_handle(adev);
  131. /* Look up for I2cSerialBus resource */
  132. INIT_LIST_HEAD(&resource_list);
  133. ret = acpi_dev_get_resources(adev, &resource_list,
  134. i2c_acpi_fill_info, lookup);
  135. acpi_dev_free_resource_list(&resource_list);
  136. if (ret < 0 || !info->addr)
  137. return -EINVAL;
  138. return 0;
  139. }
  140. static int i2c_acpi_get_info(struct acpi_device *adev,
  141. struct i2c_board_info *info,
  142. struct i2c_adapter *adapter,
  143. acpi_handle *adapter_handle)
  144. {
  145. struct list_head resource_list;
  146. struct resource_entry *entry;
  147. struct i2c_acpi_lookup lookup;
  148. int ret;
  149. memset(&lookup, 0, sizeof(lookup));
  150. lookup.info = info;
  151. ret = i2c_acpi_do_lookup(adev, &lookup);
  152. if (ret)
  153. return ret;
  154. if (adapter) {
  155. /* The adapter must match the one in I2cSerialBus() connector */
  156. if (ACPI_HANDLE(&adapter->dev) != lookup.adapter_handle)
  157. return -ENODEV;
  158. } else {
  159. struct acpi_device *adapter_adev;
  160. /* The adapter must be present */
  161. if (acpi_bus_get_device(lookup.adapter_handle, &adapter_adev))
  162. return -ENODEV;
  163. if (acpi_bus_get_status(adapter_adev) ||
  164. !adapter_adev->status.present)
  165. return -ENODEV;
  166. }
  167. info->fwnode = acpi_fwnode_handle(adev);
  168. if (adapter_handle)
  169. *adapter_handle = lookup.adapter_handle;
  170. /* Then fill IRQ number if any */
  171. INIT_LIST_HEAD(&resource_list);
  172. ret = acpi_dev_get_resources(adev, &resource_list, NULL, NULL);
  173. if (ret < 0)
  174. return -EINVAL;
  175. resource_list_for_each_entry(entry, &resource_list) {
  176. if (resource_type(entry->res) == IORESOURCE_IRQ) {
  177. info->irq = entry->res->start;
  178. break;
  179. }
  180. }
  181. acpi_dev_free_resource_list(&resource_list);
  182. strlcpy(info->type, dev_name(&adev->dev), sizeof(info->type));
  183. return 0;
  184. }
  185. static void i2c_acpi_register_device(struct i2c_adapter *adapter,
  186. struct acpi_device *adev,
  187. struct i2c_board_info *info)
  188. {
  189. adev->power.flags.ignore_parent = true;
  190. acpi_device_set_enumerated(adev);
  191. if (!i2c_new_device(adapter, info)) {
  192. adev->power.flags.ignore_parent = false;
  193. dev_err(&adapter->dev,
  194. "failed to add I2C device %s from ACPI\n",
  195. dev_name(&adev->dev));
  196. }
  197. }
  198. static acpi_status i2c_acpi_add_device(acpi_handle handle, u32 level,
  199. void *data, void **return_value)
  200. {
  201. struct i2c_adapter *adapter = data;
  202. struct acpi_device *adev;
  203. struct i2c_board_info info;
  204. if (acpi_bus_get_device(handle, &adev))
  205. return AE_OK;
  206. if (i2c_acpi_get_info(adev, &info, adapter, NULL))
  207. return AE_OK;
  208. i2c_acpi_register_device(adapter, adev, &info);
  209. return AE_OK;
  210. }
  211. #define I2C_ACPI_MAX_SCAN_DEPTH 32
  212. /**
  213. * i2c_acpi_register_devices - enumerate I2C slave devices behind adapter
  214. * @adap: pointer to adapter
  215. *
  216. * Enumerate all I2C slave devices behind this adapter by walking the ACPI
  217. * namespace. When a device is found it will be added to the Linux device
  218. * model and bound to the corresponding ACPI handle.
  219. */
  220. static void i2c_acpi_register_devices(struct i2c_adapter *adap)
  221. {
  222. acpi_status status;
  223. if (!has_acpi_companion(&adap->dev))
  224. return;
  225. status = acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
  226. I2C_ACPI_MAX_SCAN_DEPTH,
  227. i2c_acpi_add_device, NULL,
  228. adap, NULL);
  229. if (ACPI_FAILURE(status))
  230. dev_warn(&adap->dev, "failed to enumerate I2C slaves\n");
  231. }
  232. static acpi_status i2c_acpi_lookup_speed(acpi_handle handle, u32 level,
  233. void *data, void **return_value)
  234. {
  235. struct i2c_acpi_lookup *lookup = data;
  236. struct acpi_device *adev;
  237. if (acpi_bus_get_device(handle, &adev))
  238. return AE_OK;
  239. if (i2c_acpi_do_lookup(adev, lookup))
  240. return AE_OK;
  241. if (lookup->search_handle != lookup->adapter_handle)
  242. return AE_OK;
  243. if (lookup->speed <= lookup->min_speed)
  244. lookup->min_speed = lookup->speed;
  245. return AE_OK;
  246. }
  247. /**
  248. * i2c_acpi_find_bus_speed - find I2C bus speed from ACPI
  249. * @dev: The device owning the bus
  250. *
  251. * Find the I2C bus speed by walking the ACPI namespace for all I2C slaves
  252. * devices connected to this bus and use the speed of slowest device.
  253. *
  254. * Returns the speed in Hz or zero
  255. */
  256. u32 i2c_acpi_find_bus_speed(struct device *dev)
  257. {
  258. struct i2c_acpi_lookup lookup;
  259. struct i2c_board_info dummy;
  260. acpi_status status;
  261. if (!has_acpi_companion(dev))
  262. return 0;
  263. memset(&lookup, 0, sizeof(lookup));
  264. lookup.search_handle = ACPI_HANDLE(dev);
  265. lookup.min_speed = UINT_MAX;
  266. lookup.info = &dummy;
  267. status = acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
  268. I2C_ACPI_MAX_SCAN_DEPTH,
  269. i2c_acpi_lookup_speed, NULL,
  270. &lookup, NULL);
  271. if (ACPI_FAILURE(status)) {
  272. dev_warn(dev, "unable to find I2C bus speed from ACPI\n");
  273. return 0;
  274. }
  275. return lookup.min_speed != UINT_MAX ? lookup.min_speed : 0;
  276. }
  277. EXPORT_SYMBOL_GPL(i2c_acpi_find_bus_speed);
  278. static int i2c_acpi_match_adapter(struct device *dev, void *data)
  279. {
  280. struct i2c_adapter *adapter = i2c_verify_adapter(dev);
  281. if (!adapter)
  282. return 0;
  283. return ACPI_HANDLE(dev) == (acpi_handle)data;
  284. }
  285. static int i2c_acpi_match_device(struct device *dev, void *data)
  286. {
  287. return ACPI_COMPANION(dev) == data;
  288. }
  289. static struct i2c_adapter *i2c_acpi_find_adapter_by_handle(acpi_handle handle)
  290. {
  291. struct device *dev;
  292. dev = bus_find_device(&i2c_bus_type, NULL, handle,
  293. i2c_acpi_match_adapter);
  294. return dev ? i2c_verify_adapter(dev) : NULL;
  295. }
  296. static struct i2c_client *i2c_acpi_find_client_by_adev(struct acpi_device *adev)
  297. {
  298. struct device *dev;
  299. dev = bus_find_device(&i2c_bus_type, NULL, adev, i2c_acpi_match_device);
  300. return dev ? i2c_verify_client(dev) : NULL;
  301. }
  302. static int i2c_acpi_notify(struct notifier_block *nb, unsigned long value,
  303. void *arg)
  304. {
  305. struct acpi_device *adev = arg;
  306. struct i2c_board_info info;
  307. acpi_handle adapter_handle;
  308. struct i2c_adapter *adapter;
  309. struct i2c_client *client;
  310. switch (value) {
  311. case ACPI_RECONFIG_DEVICE_ADD:
  312. if (i2c_acpi_get_info(adev, &info, NULL, &adapter_handle))
  313. break;
  314. adapter = i2c_acpi_find_adapter_by_handle(adapter_handle);
  315. if (!adapter)
  316. break;
  317. i2c_acpi_register_device(adapter, adev, &info);
  318. break;
  319. case ACPI_RECONFIG_DEVICE_REMOVE:
  320. if (!acpi_device_enumerated(adev))
  321. break;
  322. client = i2c_acpi_find_client_by_adev(adev);
  323. if (!client)
  324. break;
  325. i2c_unregister_device(client);
  326. put_device(&client->dev);
  327. break;
  328. }
  329. return NOTIFY_OK;
  330. }
  331. static struct notifier_block i2c_acpi_notifier = {
  332. .notifier_call = i2c_acpi_notify,
  333. };
  334. #else /* CONFIG_ACPI */
  335. static inline void i2c_acpi_register_devices(struct i2c_adapter *adap) { }
  336. extern struct notifier_block i2c_acpi_notifier;
  337. #endif /* CONFIG_ACPI */
  338. #ifdef CONFIG_ACPI_I2C_OPREGION
  339. static int acpi_gsb_i2c_read_bytes(struct i2c_client *client,
  340. u8 cmd, u8 *data, u8 data_len)
  341. {
  342. struct i2c_msg msgs[2];
  343. int ret;
  344. u8 *buffer;
  345. buffer = kzalloc(data_len, GFP_KERNEL);
  346. if (!buffer)
  347. return AE_NO_MEMORY;
  348. msgs[0].addr = client->addr;
  349. msgs[0].flags = client->flags;
  350. msgs[0].len = 1;
  351. msgs[0].buf = &cmd;
  352. msgs[1].addr = client->addr;
  353. msgs[1].flags = client->flags | I2C_M_RD;
  354. msgs[1].len = data_len;
  355. msgs[1].buf = buffer;
  356. ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
  357. if (ret < 0)
  358. dev_err(&client->adapter->dev, "i2c read failed\n");
  359. else
  360. memcpy(data, buffer, data_len);
  361. kfree(buffer);
  362. return ret;
  363. }
  364. static int acpi_gsb_i2c_write_bytes(struct i2c_client *client,
  365. u8 cmd, u8 *data, u8 data_len)
  366. {
  367. struct i2c_msg msgs[1];
  368. u8 *buffer;
  369. int ret = AE_OK;
  370. buffer = kzalloc(data_len + 1, GFP_KERNEL);
  371. if (!buffer)
  372. return AE_NO_MEMORY;
  373. buffer[0] = cmd;
  374. memcpy(buffer + 1, data, data_len);
  375. msgs[0].addr = client->addr;
  376. msgs[0].flags = client->flags;
  377. msgs[0].len = data_len + 1;
  378. msgs[0].buf = buffer;
  379. ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
  380. if (ret < 0)
  381. dev_err(&client->adapter->dev, "i2c write failed\n");
  382. kfree(buffer);
  383. return ret;
  384. }
  385. static acpi_status
  386. i2c_acpi_space_handler(u32 function, acpi_physical_address command,
  387. u32 bits, u64 *value64,
  388. void *handler_context, void *region_context)
  389. {
  390. struct gsb_buffer *gsb = (struct gsb_buffer *)value64;
  391. struct i2c_acpi_handler_data *data = handler_context;
  392. struct acpi_connection_info *info = &data->info;
  393. struct acpi_resource_i2c_serialbus *sb;
  394. struct i2c_adapter *adapter = data->adapter;
  395. struct i2c_client *client;
  396. struct acpi_resource *ares;
  397. u32 accessor_type = function >> 16;
  398. u8 action = function & ACPI_IO_MASK;
  399. acpi_status ret;
  400. int status;
  401. ret = acpi_buffer_to_resource(info->connection, info->length, &ares);
  402. if (ACPI_FAILURE(ret))
  403. return ret;
  404. client = kzalloc(sizeof(*client), GFP_KERNEL);
  405. if (!client) {
  406. ret = AE_NO_MEMORY;
  407. goto err;
  408. }
  409. if (!value64 || ares->type != ACPI_RESOURCE_TYPE_SERIAL_BUS) {
  410. ret = AE_BAD_PARAMETER;
  411. goto err;
  412. }
  413. sb = &ares->data.i2c_serial_bus;
  414. if (sb->type != ACPI_RESOURCE_SERIAL_TYPE_I2C) {
  415. ret = AE_BAD_PARAMETER;
  416. goto err;
  417. }
  418. client->adapter = adapter;
  419. client->addr = sb->slave_address;
  420. if (sb->access_mode == ACPI_I2C_10BIT_MODE)
  421. client->flags |= I2C_CLIENT_TEN;
  422. switch (accessor_type) {
  423. case ACPI_GSB_ACCESS_ATTRIB_SEND_RCV:
  424. if (action == ACPI_READ) {
  425. status = i2c_smbus_read_byte(client);
  426. if (status >= 0) {
  427. gsb->bdata = status;
  428. status = 0;
  429. }
  430. } else {
  431. status = i2c_smbus_write_byte(client, gsb->bdata);
  432. }
  433. break;
  434. case ACPI_GSB_ACCESS_ATTRIB_BYTE:
  435. if (action == ACPI_READ) {
  436. status = i2c_smbus_read_byte_data(client, command);
  437. if (status >= 0) {
  438. gsb->bdata = status;
  439. status = 0;
  440. }
  441. } else {
  442. status = i2c_smbus_write_byte_data(client, command,
  443. gsb->bdata);
  444. }
  445. break;
  446. case ACPI_GSB_ACCESS_ATTRIB_WORD:
  447. if (action == ACPI_READ) {
  448. status = i2c_smbus_read_word_data(client, command);
  449. if (status >= 0) {
  450. gsb->wdata = status;
  451. status = 0;
  452. }
  453. } else {
  454. status = i2c_smbus_write_word_data(client, command,
  455. gsb->wdata);
  456. }
  457. break;
  458. case ACPI_GSB_ACCESS_ATTRIB_BLOCK:
  459. if (action == ACPI_READ) {
  460. status = i2c_smbus_read_block_data(client, command,
  461. gsb->data);
  462. if (status >= 0) {
  463. gsb->len = status;
  464. status = 0;
  465. }
  466. } else {
  467. status = i2c_smbus_write_block_data(client, command,
  468. gsb->len, gsb->data);
  469. }
  470. break;
  471. case ACPI_GSB_ACCESS_ATTRIB_MULTIBYTE:
  472. if (action == ACPI_READ) {
  473. status = acpi_gsb_i2c_read_bytes(client, command,
  474. gsb->data, info->access_length);
  475. if (status > 0)
  476. status = 0;
  477. } else {
  478. status = acpi_gsb_i2c_write_bytes(client, command,
  479. gsb->data, info->access_length);
  480. }
  481. break;
  482. default:
  483. dev_warn(&adapter->dev, "protocol 0x%02x not supported for client 0x%02x\n",
  484. accessor_type, client->addr);
  485. ret = AE_BAD_PARAMETER;
  486. goto err;
  487. }
  488. gsb->status = status;
  489. err:
  490. kfree(client);
  491. ACPI_FREE(ares);
  492. return ret;
  493. }
  494. static int i2c_acpi_install_space_handler(struct i2c_adapter *adapter)
  495. {
  496. acpi_handle handle;
  497. struct i2c_acpi_handler_data *data;
  498. acpi_status status;
  499. if (!adapter->dev.parent)
  500. return -ENODEV;
  501. handle = ACPI_HANDLE(adapter->dev.parent);
  502. if (!handle)
  503. return -ENODEV;
  504. data = kzalloc(sizeof(struct i2c_acpi_handler_data),
  505. GFP_KERNEL);
  506. if (!data)
  507. return -ENOMEM;
  508. data->adapter = adapter;
  509. status = acpi_bus_attach_private_data(handle, (void *)data);
  510. if (ACPI_FAILURE(status)) {
  511. kfree(data);
  512. return -ENOMEM;
  513. }
  514. status = acpi_install_address_space_handler(handle,
  515. ACPI_ADR_SPACE_GSBUS,
  516. &i2c_acpi_space_handler,
  517. NULL,
  518. data);
  519. if (ACPI_FAILURE(status)) {
  520. dev_err(&adapter->dev, "Error installing i2c space handler\n");
  521. acpi_bus_detach_private_data(handle);
  522. kfree(data);
  523. return -ENOMEM;
  524. }
  525. acpi_walk_dep_device_list(handle);
  526. return 0;
  527. }
  528. static void i2c_acpi_remove_space_handler(struct i2c_adapter *adapter)
  529. {
  530. acpi_handle handle;
  531. struct i2c_acpi_handler_data *data;
  532. acpi_status status;
  533. if (!adapter->dev.parent)
  534. return;
  535. handle = ACPI_HANDLE(adapter->dev.parent);
  536. if (!handle)
  537. return;
  538. acpi_remove_address_space_handler(handle,
  539. ACPI_ADR_SPACE_GSBUS,
  540. &i2c_acpi_space_handler);
  541. status = acpi_bus_get_private_data(handle, (void **)&data);
  542. if (ACPI_SUCCESS(status))
  543. kfree(data);
  544. acpi_bus_detach_private_data(handle);
  545. }
  546. #else /* CONFIG_ACPI_I2C_OPREGION */
  547. static inline void i2c_acpi_remove_space_handler(struct i2c_adapter *adapter)
  548. { }
  549. static inline int i2c_acpi_install_space_handler(struct i2c_adapter *adapter)
  550. { return 0; }
  551. #endif /* CONFIG_ACPI_I2C_OPREGION */
  552. /* ------------------------------------------------------------------------- */
  553. static const struct i2c_device_id *i2c_match_id(const struct i2c_device_id *id,
  554. const struct i2c_client *client)
  555. {
  556. while (id->name[0]) {
  557. if (strcmp(client->name, id->name) == 0)
  558. return id;
  559. id++;
  560. }
  561. return NULL;
  562. }
  563. static int i2c_device_match(struct device *dev, struct device_driver *drv)
  564. {
  565. struct i2c_client *client = i2c_verify_client(dev);
  566. struct i2c_driver *driver;
  567. if (!client)
  568. return 0;
  569. /* Attempt an OF style match */
  570. if (of_driver_match_device(dev, drv))
  571. return 1;
  572. /* Then ACPI style match */
  573. if (acpi_driver_match_device(dev, drv))
  574. return 1;
  575. driver = to_i2c_driver(drv);
  576. /* match on an id table if there is one */
  577. if (driver->id_table)
  578. return i2c_match_id(driver->id_table, client) != NULL;
  579. return 0;
  580. }
  581. static int i2c_device_uevent(struct device *dev, struct kobj_uevent_env *env)
  582. {
  583. struct i2c_client *client = to_i2c_client(dev);
  584. int rc;
  585. rc = acpi_device_uevent_modalias(dev, env);
  586. if (rc != -ENODEV)
  587. return rc;
  588. return add_uevent_var(env, "MODALIAS=%s%s", I2C_MODULE_PREFIX, client->name);
  589. }
  590. /* i2c bus recovery routines */
  591. static int get_scl_gpio_value(struct i2c_adapter *adap)
  592. {
  593. return gpio_get_value(adap->bus_recovery_info->scl_gpio);
  594. }
  595. static void set_scl_gpio_value(struct i2c_adapter *adap, int val)
  596. {
  597. gpio_set_value(adap->bus_recovery_info->scl_gpio, val);
  598. }
  599. static int get_sda_gpio_value(struct i2c_adapter *adap)
  600. {
  601. return gpio_get_value(adap->bus_recovery_info->sda_gpio);
  602. }
  603. static int i2c_get_gpios_for_recovery(struct i2c_adapter *adap)
  604. {
  605. struct i2c_bus_recovery_info *bri = adap->bus_recovery_info;
  606. struct device *dev = &adap->dev;
  607. int ret = 0;
  608. ret = gpio_request_one(bri->scl_gpio, GPIOF_OPEN_DRAIN |
  609. GPIOF_OUT_INIT_HIGH, "i2c-scl");
  610. if (ret) {
  611. dev_warn(dev, "Can't get SCL gpio: %d\n", bri->scl_gpio);
  612. return ret;
  613. }
  614. if (bri->get_sda) {
  615. if (gpio_request_one(bri->sda_gpio, GPIOF_IN, "i2c-sda")) {
  616. /* work without SDA polling */
  617. dev_warn(dev, "Can't get SDA gpio: %d. Not using SDA polling\n",
  618. bri->sda_gpio);
  619. bri->get_sda = NULL;
  620. }
  621. }
  622. return ret;
  623. }
  624. static void i2c_put_gpios_for_recovery(struct i2c_adapter *adap)
  625. {
  626. struct i2c_bus_recovery_info *bri = adap->bus_recovery_info;
  627. if (bri->get_sda)
  628. gpio_free(bri->sda_gpio);
  629. gpio_free(bri->scl_gpio);
  630. }
  631. /*
  632. * We are generating clock pulses. ndelay() determines durating of clk pulses.
  633. * We will generate clock with rate 100 KHz and so duration of both clock levels
  634. * is: delay in ns = (10^6 / 100) / 2
  635. */
  636. #define RECOVERY_NDELAY 5000
  637. #define RECOVERY_CLK_CNT 9
  638. static int i2c_generic_recovery(struct i2c_adapter *adap)
  639. {
  640. struct i2c_bus_recovery_info *bri = adap->bus_recovery_info;
  641. int i = 0, val = 1, ret = 0;
  642. if (bri->prepare_recovery)
  643. bri->prepare_recovery(adap);
  644. bri->set_scl(adap, val);
  645. ndelay(RECOVERY_NDELAY);
  646. /*
  647. * By this time SCL is high, as we need to give 9 falling-rising edges
  648. */
  649. while (i++ < RECOVERY_CLK_CNT * 2) {
  650. if (val) {
  651. /* Break if SDA is high */
  652. if (bri->get_sda && bri->get_sda(adap))
  653. break;
  654. /* SCL shouldn't be low here */
  655. if (!bri->get_scl(adap)) {
  656. dev_err(&adap->dev,
  657. "SCL is stuck low, exit recovery\n");
  658. ret = -EBUSY;
  659. break;
  660. }
  661. }
  662. val = !val;
  663. bri->set_scl(adap, val);
  664. ndelay(RECOVERY_NDELAY);
  665. }
  666. if (bri->unprepare_recovery)
  667. bri->unprepare_recovery(adap);
  668. return ret;
  669. }
  670. int i2c_generic_scl_recovery(struct i2c_adapter *adap)
  671. {
  672. return i2c_generic_recovery(adap);
  673. }
  674. EXPORT_SYMBOL_GPL(i2c_generic_scl_recovery);
  675. int i2c_generic_gpio_recovery(struct i2c_adapter *adap)
  676. {
  677. int ret;
  678. ret = i2c_get_gpios_for_recovery(adap);
  679. if (ret)
  680. return ret;
  681. ret = i2c_generic_recovery(adap);
  682. i2c_put_gpios_for_recovery(adap);
  683. return ret;
  684. }
  685. EXPORT_SYMBOL_GPL(i2c_generic_gpio_recovery);
  686. int i2c_recover_bus(struct i2c_adapter *adap)
  687. {
  688. if (!adap->bus_recovery_info)
  689. return -EOPNOTSUPP;
  690. dev_dbg(&adap->dev, "Trying i2c bus recovery\n");
  691. return adap->bus_recovery_info->recover_bus(adap);
  692. }
  693. EXPORT_SYMBOL_GPL(i2c_recover_bus);
  694. static void i2c_init_recovery(struct i2c_adapter *adap)
  695. {
  696. struct i2c_bus_recovery_info *bri = adap->bus_recovery_info;
  697. char *err_str;
  698. if (!bri)
  699. return;
  700. if (!bri->recover_bus) {
  701. err_str = "no recover_bus() found";
  702. goto err;
  703. }
  704. /* Generic GPIO recovery */
  705. if (bri->recover_bus == i2c_generic_gpio_recovery) {
  706. if (!gpio_is_valid(bri->scl_gpio)) {
  707. err_str = "invalid SCL gpio";
  708. goto err;
  709. }
  710. if (gpio_is_valid(bri->sda_gpio))
  711. bri->get_sda = get_sda_gpio_value;
  712. else
  713. bri->get_sda = NULL;
  714. bri->get_scl = get_scl_gpio_value;
  715. bri->set_scl = set_scl_gpio_value;
  716. } else if (bri->recover_bus == i2c_generic_scl_recovery) {
  717. /* Generic SCL recovery */
  718. if (!bri->set_scl || !bri->get_scl) {
  719. err_str = "no {get|set}_scl() found";
  720. goto err;
  721. }
  722. }
  723. return;
  724. err:
  725. dev_err(&adap->dev, "Not using recovery: %s\n", err_str);
  726. adap->bus_recovery_info = NULL;
  727. }
  728. static int i2c_device_probe(struct device *dev)
  729. {
  730. struct i2c_client *client = i2c_verify_client(dev);
  731. struct i2c_driver *driver;
  732. int status;
  733. if (!client)
  734. return 0;
  735. if (!client->irq) {
  736. int irq = -ENOENT;
  737. if (dev->of_node) {
  738. irq = of_irq_get_byname(dev->of_node, "irq");
  739. if (irq == -EINVAL || irq == -ENODATA)
  740. irq = of_irq_get(dev->of_node, 0);
  741. } else if (ACPI_COMPANION(dev)) {
  742. irq = acpi_dev_gpio_irq_get(ACPI_COMPANION(dev), 0);
  743. }
  744. if (irq == -EPROBE_DEFER)
  745. return irq;
  746. if (irq < 0)
  747. irq = 0;
  748. client->irq = irq;
  749. }
  750. driver = to_i2c_driver(dev->driver);
  751. if (!driver->probe || !driver->id_table)
  752. return -ENODEV;
  753. if (client->flags & I2C_CLIENT_WAKE) {
  754. int wakeirq = -ENOENT;
  755. if (dev->of_node) {
  756. wakeirq = of_irq_get_byname(dev->of_node, "wakeup");
  757. if (wakeirq == -EPROBE_DEFER)
  758. return wakeirq;
  759. }
  760. device_init_wakeup(&client->dev, true);
  761. if (wakeirq > 0 && wakeirq != client->irq)
  762. status = dev_pm_set_dedicated_wake_irq(dev, wakeirq);
  763. else if (client->irq > 0)
  764. status = dev_pm_set_wake_irq(dev, client->irq);
  765. else
  766. status = 0;
  767. if (status)
  768. dev_warn(&client->dev, "failed to set up wakeup irq\n");
  769. }
  770. dev_dbg(dev, "probe\n");
  771. status = of_clk_set_defaults(dev->of_node, false);
  772. if (status < 0)
  773. goto err_clear_wakeup_irq;
  774. status = dev_pm_domain_attach(&client->dev, true);
  775. if (status == -EPROBE_DEFER)
  776. goto err_clear_wakeup_irq;
  777. status = driver->probe(client, i2c_match_id(driver->id_table, client));
  778. if (status)
  779. goto err_detach_pm_domain;
  780. return 0;
  781. err_detach_pm_domain:
  782. dev_pm_domain_detach(&client->dev, true);
  783. err_clear_wakeup_irq:
  784. dev_pm_clear_wake_irq(&client->dev);
  785. device_init_wakeup(&client->dev, false);
  786. return status;
  787. }
  788. static int i2c_device_remove(struct device *dev)
  789. {
  790. struct i2c_client *client = i2c_verify_client(dev);
  791. struct i2c_driver *driver;
  792. int status = 0;
  793. if (!client || !dev->driver)
  794. return 0;
  795. driver = to_i2c_driver(dev->driver);
  796. if (driver->remove) {
  797. dev_dbg(dev, "remove\n");
  798. status = driver->remove(client);
  799. }
  800. dev_pm_domain_detach(&client->dev, true);
  801. dev_pm_clear_wake_irq(&client->dev);
  802. device_init_wakeup(&client->dev, false);
  803. return status;
  804. }
  805. static void i2c_device_shutdown(struct device *dev)
  806. {
  807. struct i2c_client *client = i2c_verify_client(dev);
  808. struct i2c_driver *driver;
  809. if (!client || !dev->driver)
  810. return;
  811. driver = to_i2c_driver(dev->driver);
  812. if (driver->shutdown)
  813. driver->shutdown(client);
  814. }
  815. static void i2c_client_dev_release(struct device *dev)
  816. {
  817. kfree(to_i2c_client(dev));
  818. }
  819. static ssize_t
  820. show_name(struct device *dev, struct device_attribute *attr, char *buf)
  821. {
  822. return sprintf(buf, "%s\n", dev->type == &i2c_client_type ?
  823. to_i2c_client(dev)->name : to_i2c_adapter(dev)->name);
  824. }
  825. static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
  826. static ssize_t
  827. show_modalias(struct device *dev, struct device_attribute *attr, char *buf)
  828. {
  829. struct i2c_client *client = to_i2c_client(dev);
  830. int len;
  831. len = acpi_device_modalias(dev, buf, PAGE_SIZE -1);
  832. if (len != -ENODEV)
  833. return len;
  834. return sprintf(buf, "%s%s\n", I2C_MODULE_PREFIX, client->name);
  835. }
  836. static DEVICE_ATTR(modalias, S_IRUGO, show_modalias, NULL);
  837. static struct attribute *i2c_dev_attrs[] = {
  838. &dev_attr_name.attr,
  839. /* modalias helps coldplug: modprobe $(cat .../modalias) */
  840. &dev_attr_modalias.attr,
  841. NULL
  842. };
  843. ATTRIBUTE_GROUPS(i2c_dev);
  844. struct bus_type i2c_bus_type = {
  845. .name = "i2c",
  846. .match = i2c_device_match,
  847. .probe = i2c_device_probe,
  848. .remove = i2c_device_remove,
  849. .shutdown = i2c_device_shutdown,
  850. };
  851. EXPORT_SYMBOL_GPL(i2c_bus_type);
  852. static struct device_type i2c_client_type = {
  853. .groups = i2c_dev_groups,
  854. .uevent = i2c_device_uevent,
  855. .release = i2c_client_dev_release,
  856. };
  857. /**
  858. * i2c_verify_client - return parameter as i2c_client, or NULL
  859. * @dev: device, probably from some driver model iterator
  860. *
  861. * When traversing the driver model tree, perhaps using driver model
  862. * iterators like @device_for_each_child(), you can't assume very much
  863. * about the nodes you find. Use this function to avoid oopses caused
  864. * by wrongly treating some non-I2C device as an i2c_client.
  865. */
  866. struct i2c_client *i2c_verify_client(struct device *dev)
  867. {
  868. return (dev->type == &i2c_client_type)
  869. ? to_i2c_client(dev)
  870. : NULL;
  871. }
  872. EXPORT_SYMBOL(i2c_verify_client);
  873. /* Return a unique address which takes the flags of the client into account */
  874. static unsigned short i2c_encode_flags_to_addr(struct i2c_client *client)
  875. {
  876. unsigned short addr = client->addr;
  877. /* For some client flags, add an arbitrary offset to avoid collisions */
  878. if (client->flags & I2C_CLIENT_TEN)
  879. addr |= I2C_ADDR_OFFSET_TEN_BIT;
  880. if (client->flags & I2C_CLIENT_SLAVE)
  881. addr |= I2C_ADDR_OFFSET_SLAVE;
  882. return addr;
  883. }
  884. /* This is a permissive address validity check, I2C address map constraints
  885. * are purposely not enforced, except for the general call address. */
  886. static int i2c_check_addr_validity(unsigned addr, unsigned short flags)
  887. {
  888. if (flags & I2C_CLIENT_TEN) {
  889. /* 10-bit address, all values are valid */
  890. if (addr > 0x3ff)
  891. return -EINVAL;
  892. } else {
  893. /* 7-bit address, reject the general call address */
  894. if (addr == 0x00 || addr > 0x7f)
  895. return -EINVAL;
  896. }
  897. return 0;
  898. }
  899. /* And this is a strict address validity check, used when probing. If a
  900. * device uses a reserved address, then it shouldn't be probed. 7-bit
  901. * addressing is assumed, 10-bit address devices are rare and should be
  902. * explicitly enumerated. */
  903. static int i2c_check_7bit_addr_validity_strict(unsigned short addr)
  904. {
  905. /*
  906. * Reserved addresses per I2C specification:
  907. * 0x00 General call address / START byte
  908. * 0x01 CBUS address
  909. * 0x02 Reserved for different bus format
  910. * 0x03 Reserved for future purposes
  911. * 0x04-0x07 Hs-mode master code
  912. * 0x78-0x7b 10-bit slave addressing
  913. * 0x7c-0x7f Reserved for future purposes
  914. */
  915. if (addr < 0x08 || addr > 0x77)
  916. return -EINVAL;
  917. return 0;
  918. }
  919. static int __i2c_check_addr_busy(struct device *dev, void *addrp)
  920. {
  921. struct i2c_client *client = i2c_verify_client(dev);
  922. int addr = *(int *)addrp;
  923. if (client && i2c_encode_flags_to_addr(client) == addr)
  924. return -EBUSY;
  925. return 0;
  926. }
  927. /* walk up mux tree */
  928. static int i2c_check_mux_parents(struct i2c_adapter *adapter, int addr)
  929. {
  930. struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
  931. int result;
  932. result = device_for_each_child(&adapter->dev, &addr,
  933. __i2c_check_addr_busy);
  934. if (!result && parent)
  935. result = i2c_check_mux_parents(parent, addr);
  936. return result;
  937. }
  938. /* recurse down mux tree */
  939. static int i2c_check_mux_children(struct device *dev, void *addrp)
  940. {
  941. int result;
  942. if (dev->type == &i2c_adapter_type)
  943. result = device_for_each_child(dev, addrp,
  944. i2c_check_mux_children);
  945. else
  946. result = __i2c_check_addr_busy(dev, addrp);
  947. return result;
  948. }
  949. static int i2c_check_addr_busy(struct i2c_adapter *adapter, int addr)
  950. {
  951. struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
  952. int result = 0;
  953. if (parent)
  954. result = i2c_check_mux_parents(parent, addr);
  955. if (!result)
  956. result = device_for_each_child(&adapter->dev, &addr,
  957. i2c_check_mux_children);
  958. return result;
  959. }
  960. /**
  961. * i2c_adapter_lock_bus - Get exclusive access to an I2C bus segment
  962. * @adapter: Target I2C bus segment
  963. * @flags: I2C_LOCK_ROOT_ADAPTER locks the root i2c adapter, I2C_LOCK_SEGMENT
  964. * locks only this branch in the adapter tree
  965. */
  966. static void i2c_adapter_lock_bus(struct i2c_adapter *adapter,
  967. unsigned int flags)
  968. {
  969. rt_mutex_lock(&adapter->bus_lock);
  970. }
  971. /**
  972. * i2c_adapter_trylock_bus - Try to get exclusive access to an I2C bus segment
  973. * @adapter: Target I2C bus segment
  974. * @flags: I2C_LOCK_ROOT_ADAPTER trylocks the root i2c adapter, I2C_LOCK_SEGMENT
  975. * trylocks only this branch in the adapter tree
  976. */
  977. static int i2c_adapter_trylock_bus(struct i2c_adapter *adapter,
  978. unsigned int flags)
  979. {
  980. return rt_mutex_trylock(&adapter->bus_lock);
  981. }
  982. /**
  983. * i2c_adapter_unlock_bus - Release exclusive access to an I2C bus segment
  984. * @adapter: Target I2C bus segment
  985. * @flags: I2C_LOCK_ROOT_ADAPTER unlocks the root i2c adapter, I2C_LOCK_SEGMENT
  986. * unlocks only this branch in the adapter tree
  987. */
  988. static void i2c_adapter_unlock_bus(struct i2c_adapter *adapter,
  989. unsigned int flags)
  990. {
  991. rt_mutex_unlock(&adapter->bus_lock);
  992. }
  993. static void i2c_dev_set_name(struct i2c_adapter *adap,
  994. struct i2c_client *client)
  995. {
  996. struct acpi_device *adev = ACPI_COMPANION(&client->dev);
  997. if (adev) {
  998. dev_set_name(&client->dev, "i2c-%s", acpi_dev_name(adev));
  999. return;
  1000. }
  1001. dev_set_name(&client->dev, "%d-%04x", i2c_adapter_id(adap),
  1002. i2c_encode_flags_to_addr(client));
  1003. }
  1004. /**
  1005. * i2c_new_device - instantiate an i2c device
  1006. * @adap: the adapter managing the device
  1007. * @info: describes one I2C device; bus_num is ignored
  1008. * Context: can sleep
  1009. *
  1010. * Create an i2c device. Binding is handled through driver model
  1011. * probe()/remove() methods. A driver may be bound to this device when we
  1012. * return from this function, or any later moment (e.g. maybe hotplugging will
  1013. * load the driver module). This call is not appropriate for use by mainboard
  1014. * initialization logic, which usually runs during an arch_initcall() long
  1015. * before any i2c_adapter could exist.
  1016. *
  1017. * This returns the new i2c client, which may be saved for later use with
  1018. * i2c_unregister_device(); or NULL to indicate an error.
  1019. */
  1020. struct i2c_client *
  1021. i2c_new_device(struct i2c_adapter *adap, struct i2c_board_info const *info)
  1022. {
  1023. struct i2c_client *client;
  1024. int status;
  1025. client = kzalloc(sizeof *client, GFP_KERNEL);
  1026. if (!client)
  1027. return NULL;
  1028. client->adapter = adap;
  1029. client->dev.platform_data = info->platform_data;
  1030. if (info->archdata)
  1031. client->dev.archdata = *info->archdata;
  1032. client->flags = info->flags;
  1033. client->addr = info->addr;
  1034. client->irq = info->irq;
  1035. strlcpy(client->name, info->type, sizeof(client->name));
  1036. status = i2c_check_addr_validity(client->addr, client->flags);
  1037. if (status) {
  1038. dev_err(&adap->dev, "Invalid %d-bit I2C address 0x%02hx\n",
  1039. client->flags & I2C_CLIENT_TEN ? 10 : 7, client->addr);
  1040. goto out_err_silent;
  1041. }
  1042. /* Check for address business */
  1043. status = i2c_check_addr_busy(adap, i2c_encode_flags_to_addr(client));
  1044. if (status)
  1045. goto out_err;
  1046. client->dev.parent = &client->adapter->dev;
  1047. client->dev.bus = &i2c_bus_type;
  1048. client->dev.type = &i2c_client_type;
  1049. client->dev.of_node = info->of_node;
  1050. client->dev.fwnode = info->fwnode;
  1051. i2c_dev_set_name(adap, client);
  1052. status = device_register(&client->dev);
  1053. if (status)
  1054. goto out_err;
  1055. dev_dbg(&adap->dev, "client [%s] registered with bus id %s\n",
  1056. client->name, dev_name(&client->dev));
  1057. return client;
  1058. out_err:
  1059. dev_err(&adap->dev,
  1060. "Failed to register i2c client %s at 0x%02x (%d)\n",
  1061. client->name, client->addr, status);
  1062. out_err_silent:
  1063. kfree(client);
  1064. return NULL;
  1065. }
  1066. EXPORT_SYMBOL_GPL(i2c_new_device);
  1067. /**
  1068. * i2c_unregister_device - reverse effect of i2c_new_device()
  1069. * @client: value returned from i2c_new_device()
  1070. * Context: can sleep
  1071. */
  1072. void i2c_unregister_device(struct i2c_client *client)
  1073. {
  1074. if (client->dev.of_node)
  1075. of_node_clear_flag(client->dev.of_node, OF_POPULATED);
  1076. if (ACPI_COMPANION(&client->dev))
  1077. acpi_device_clear_enumerated(ACPI_COMPANION(&client->dev));
  1078. device_unregister(&client->dev);
  1079. }
  1080. EXPORT_SYMBOL_GPL(i2c_unregister_device);
  1081. static const struct i2c_device_id dummy_id[] = {
  1082. { "dummy", 0 },
  1083. { },
  1084. };
  1085. static int dummy_probe(struct i2c_client *client,
  1086. const struct i2c_device_id *id)
  1087. {
  1088. return 0;
  1089. }
  1090. static int dummy_remove(struct i2c_client *client)
  1091. {
  1092. return 0;
  1093. }
  1094. static struct i2c_driver dummy_driver = {
  1095. .driver.name = "dummy",
  1096. .probe = dummy_probe,
  1097. .remove = dummy_remove,
  1098. .id_table = dummy_id,
  1099. };
  1100. /**
  1101. * i2c_new_dummy - return a new i2c device bound to a dummy driver
  1102. * @adapter: the adapter managing the device
  1103. * @address: seven bit address to be used
  1104. * Context: can sleep
  1105. *
  1106. * This returns an I2C client bound to the "dummy" driver, intended for use
  1107. * with devices that consume multiple addresses. Examples of such chips
  1108. * include various EEPROMS (like 24c04 and 24c08 models).
  1109. *
  1110. * These dummy devices have two main uses. First, most I2C and SMBus calls
  1111. * except i2c_transfer() need a client handle; the dummy will be that handle.
  1112. * And second, this prevents the specified address from being bound to a
  1113. * different driver.
  1114. *
  1115. * This returns the new i2c client, which should be saved for later use with
  1116. * i2c_unregister_device(); or NULL to indicate an error.
  1117. */
  1118. struct i2c_client *i2c_new_dummy(struct i2c_adapter *adapter, u16 address)
  1119. {
  1120. struct i2c_board_info info = {
  1121. I2C_BOARD_INFO("dummy", address),
  1122. };
  1123. return i2c_new_device(adapter, &info);
  1124. }
  1125. EXPORT_SYMBOL_GPL(i2c_new_dummy);
  1126. /**
  1127. * i2c_new_secondary_device - Helper to get the instantiated secondary address
  1128. * and create the associated device
  1129. * @client: Handle to the primary client
  1130. * @name: Handle to specify which secondary address to get
  1131. * @default_addr: Used as a fallback if no secondary address was specified
  1132. * Context: can sleep
  1133. *
  1134. * I2C clients can be composed of multiple I2C slaves bound together in a single
  1135. * component. The I2C client driver then binds to the master I2C slave and needs
  1136. * to create I2C dummy clients to communicate with all the other slaves.
  1137. *
  1138. * This function creates and returns an I2C dummy client whose I2C address is
  1139. * retrieved from the platform firmware based on the given slave name. If no
  1140. * address is specified by the firmware default_addr is used.
  1141. *
  1142. * On DT-based platforms the address is retrieved from the "reg" property entry
  1143. * cell whose "reg-names" value matches the slave name.
  1144. *
  1145. * This returns the new i2c client, which should be saved for later use with
  1146. * i2c_unregister_device(); or NULL to indicate an error.
  1147. */
  1148. struct i2c_client *i2c_new_secondary_device(struct i2c_client *client,
  1149. const char *name,
  1150. u16 default_addr)
  1151. {
  1152. struct device_node *np = client->dev.of_node;
  1153. u32 addr = default_addr;
  1154. int i;
  1155. if (np) {
  1156. i = of_property_match_string(np, "reg-names", name);
  1157. if (i >= 0)
  1158. of_property_read_u32_index(np, "reg", i, &addr);
  1159. }
  1160. dev_dbg(&client->adapter->dev, "Address for %s : 0x%x\n", name, addr);
  1161. return i2c_new_dummy(client->adapter, addr);
  1162. }
  1163. EXPORT_SYMBOL_GPL(i2c_new_secondary_device);
  1164. /* ------------------------------------------------------------------------- */
  1165. /* I2C bus adapters -- one roots each I2C or SMBUS segment */
  1166. static void i2c_adapter_dev_release(struct device *dev)
  1167. {
  1168. struct i2c_adapter *adap = to_i2c_adapter(dev);
  1169. complete(&adap->dev_released);
  1170. }
  1171. unsigned int i2c_adapter_depth(struct i2c_adapter *adapter)
  1172. {
  1173. unsigned int depth = 0;
  1174. while ((adapter = i2c_parent_is_i2c_adapter(adapter)))
  1175. depth++;
  1176. WARN_ONCE(depth >= MAX_LOCKDEP_SUBCLASSES,
  1177. "adapter depth exceeds lockdep subclass limit\n");
  1178. return depth;
  1179. }
  1180. EXPORT_SYMBOL_GPL(i2c_adapter_depth);
  1181. /*
  1182. * Let users instantiate I2C devices through sysfs. This can be used when
  1183. * platform initialization code doesn't contain the proper data for
  1184. * whatever reason. Also useful for drivers that do device detection and
  1185. * detection fails, either because the device uses an unexpected address,
  1186. * or this is a compatible device with different ID register values.
  1187. *
  1188. * Parameter checking may look overzealous, but we really don't want
  1189. * the user to provide incorrect parameters.
  1190. */
  1191. static ssize_t
  1192. i2c_sysfs_new_device(struct device *dev, struct device_attribute *attr,
  1193. const char *buf, size_t count)
  1194. {
  1195. struct i2c_adapter *adap = to_i2c_adapter(dev);
  1196. struct i2c_board_info info;
  1197. struct i2c_client *client;
  1198. char *blank, end;
  1199. int res;
  1200. memset(&info, 0, sizeof(struct i2c_board_info));
  1201. blank = strchr(buf, ' ');
  1202. if (!blank) {
  1203. dev_err(dev, "%s: Missing parameters\n", "new_device");
  1204. return -EINVAL;
  1205. }
  1206. if (blank - buf > I2C_NAME_SIZE - 1) {
  1207. dev_err(dev, "%s: Invalid device name\n", "new_device");
  1208. return -EINVAL;
  1209. }
  1210. memcpy(info.type, buf, blank - buf);
  1211. /* Parse remaining parameters, reject extra parameters */
  1212. res = sscanf(++blank, "%hi%c", &info.addr, &end);
  1213. if (res < 1) {
  1214. dev_err(dev, "%s: Can't parse I2C address\n", "new_device");
  1215. return -EINVAL;
  1216. }
  1217. if (res > 1 && end != '\n') {
  1218. dev_err(dev, "%s: Extra parameters\n", "new_device");
  1219. return -EINVAL;
  1220. }
  1221. if ((info.addr & I2C_ADDR_OFFSET_TEN_BIT) == I2C_ADDR_OFFSET_TEN_BIT) {
  1222. info.addr &= ~I2C_ADDR_OFFSET_TEN_BIT;
  1223. info.flags |= I2C_CLIENT_TEN;
  1224. }
  1225. if (info.addr & I2C_ADDR_OFFSET_SLAVE) {
  1226. info.addr &= ~I2C_ADDR_OFFSET_SLAVE;
  1227. info.flags |= I2C_CLIENT_SLAVE;
  1228. }
  1229. client = i2c_new_device(adap, &info);
  1230. if (!client)
  1231. return -EINVAL;
  1232. /* Keep track of the added device */
  1233. mutex_lock(&adap->userspace_clients_lock);
  1234. list_add_tail(&client->detected, &adap->userspace_clients);
  1235. mutex_unlock(&adap->userspace_clients_lock);
  1236. dev_info(dev, "%s: Instantiated device %s at 0x%02hx\n", "new_device",
  1237. info.type, info.addr);
  1238. return count;
  1239. }
  1240. static DEVICE_ATTR(new_device, S_IWUSR, NULL, i2c_sysfs_new_device);
  1241. /*
  1242. * And of course let the users delete the devices they instantiated, if
  1243. * they got it wrong. This interface can only be used to delete devices
  1244. * instantiated by i2c_sysfs_new_device above. This guarantees that we
  1245. * don't delete devices to which some kernel code still has references.
  1246. *
  1247. * Parameter checking may look overzealous, but we really don't want
  1248. * the user to delete the wrong device.
  1249. */
  1250. static ssize_t
  1251. i2c_sysfs_delete_device(struct device *dev, struct device_attribute *attr,
  1252. const char *buf, size_t count)
  1253. {
  1254. struct i2c_adapter *adap = to_i2c_adapter(dev);
  1255. struct i2c_client *client, *next;
  1256. unsigned short addr;
  1257. char end;
  1258. int res;
  1259. /* Parse parameters, reject extra parameters */
  1260. res = sscanf(buf, "%hi%c", &addr, &end);
  1261. if (res < 1) {
  1262. dev_err(dev, "%s: Can't parse I2C address\n", "delete_device");
  1263. return -EINVAL;
  1264. }
  1265. if (res > 1 && end != '\n') {
  1266. dev_err(dev, "%s: Extra parameters\n", "delete_device");
  1267. return -EINVAL;
  1268. }
  1269. /* Make sure the device was added through sysfs */
  1270. res = -ENOENT;
  1271. mutex_lock_nested(&adap->userspace_clients_lock,
  1272. i2c_adapter_depth(adap));
  1273. list_for_each_entry_safe(client, next, &adap->userspace_clients,
  1274. detected) {
  1275. if (i2c_encode_flags_to_addr(client) == addr) {
  1276. dev_info(dev, "%s: Deleting device %s at 0x%02hx\n",
  1277. "delete_device", client->name, client->addr);
  1278. list_del(&client->detected);
  1279. i2c_unregister_device(client);
  1280. res = count;
  1281. break;
  1282. }
  1283. }
  1284. mutex_unlock(&adap->userspace_clients_lock);
  1285. if (res < 0)
  1286. dev_err(dev, "%s: Can't find device in list\n",
  1287. "delete_device");
  1288. return res;
  1289. }
  1290. static DEVICE_ATTR_IGNORE_LOCKDEP(delete_device, S_IWUSR, NULL,
  1291. i2c_sysfs_delete_device);
  1292. static struct attribute *i2c_adapter_attrs[] = {
  1293. &dev_attr_name.attr,
  1294. &dev_attr_new_device.attr,
  1295. &dev_attr_delete_device.attr,
  1296. NULL
  1297. };
  1298. ATTRIBUTE_GROUPS(i2c_adapter);
  1299. struct device_type i2c_adapter_type = {
  1300. .groups = i2c_adapter_groups,
  1301. .release = i2c_adapter_dev_release,
  1302. };
  1303. EXPORT_SYMBOL_GPL(i2c_adapter_type);
  1304. /**
  1305. * i2c_verify_adapter - return parameter as i2c_adapter or NULL
  1306. * @dev: device, probably from some driver model iterator
  1307. *
  1308. * When traversing the driver model tree, perhaps using driver model
  1309. * iterators like @device_for_each_child(), you can't assume very much
  1310. * about the nodes you find. Use this function to avoid oopses caused
  1311. * by wrongly treating some non-I2C device as an i2c_adapter.
  1312. */
  1313. struct i2c_adapter *i2c_verify_adapter(struct device *dev)
  1314. {
  1315. return (dev->type == &i2c_adapter_type)
  1316. ? to_i2c_adapter(dev)
  1317. : NULL;
  1318. }
  1319. EXPORT_SYMBOL(i2c_verify_adapter);
  1320. #ifdef CONFIG_I2C_COMPAT
  1321. static struct class_compat *i2c_adapter_compat_class;
  1322. #endif
  1323. static void i2c_scan_static_board_info(struct i2c_adapter *adapter)
  1324. {
  1325. struct i2c_devinfo *devinfo;
  1326. down_read(&__i2c_board_lock);
  1327. list_for_each_entry(devinfo, &__i2c_board_list, list) {
  1328. if (devinfo->busnum == adapter->nr
  1329. && !i2c_new_device(adapter,
  1330. &devinfo->board_info))
  1331. dev_err(&adapter->dev,
  1332. "Can't create device at 0x%02x\n",
  1333. devinfo->board_info.addr);
  1334. }
  1335. up_read(&__i2c_board_lock);
  1336. }
  1337. /* OF support code */
  1338. #if IS_ENABLED(CONFIG_OF)
  1339. static struct i2c_client *of_i2c_register_device(struct i2c_adapter *adap,
  1340. struct device_node *node)
  1341. {
  1342. struct i2c_client *result;
  1343. struct i2c_board_info info = {};
  1344. struct dev_archdata dev_ad = {};
  1345. const __be32 *addr_be;
  1346. u32 addr;
  1347. int len;
  1348. dev_dbg(&adap->dev, "of_i2c: register %s\n", node->full_name);
  1349. if (of_modalias_node(node, info.type, sizeof(info.type)) < 0) {
  1350. dev_err(&adap->dev, "of_i2c: modalias failure on %s\n",
  1351. node->full_name);
  1352. return ERR_PTR(-EINVAL);
  1353. }
  1354. addr_be = of_get_property(node, "reg", &len);
  1355. if (!addr_be || (len < sizeof(*addr_be))) {
  1356. dev_err(&adap->dev, "of_i2c: invalid reg on %s\n",
  1357. node->full_name);
  1358. return ERR_PTR(-EINVAL);
  1359. }
  1360. addr = be32_to_cpup(addr_be);
  1361. if (addr & I2C_TEN_BIT_ADDRESS) {
  1362. addr &= ~I2C_TEN_BIT_ADDRESS;
  1363. info.flags |= I2C_CLIENT_TEN;
  1364. }
  1365. if (addr & I2C_OWN_SLAVE_ADDRESS) {
  1366. addr &= ~I2C_OWN_SLAVE_ADDRESS;
  1367. info.flags |= I2C_CLIENT_SLAVE;
  1368. }
  1369. if (i2c_check_addr_validity(addr, info.flags)) {
  1370. dev_err(&adap->dev, "of_i2c: invalid addr=%x on %s\n",
  1371. addr, node->full_name);
  1372. return ERR_PTR(-EINVAL);
  1373. }
  1374. info.addr = addr;
  1375. info.of_node = of_node_get(node);
  1376. info.archdata = &dev_ad;
  1377. if (of_get_property(node, "wakeup-source", NULL))
  1378. info.flags |= I2C_CLIENT_WAKE;
  1379. result = i2c_new_device(adap, &info);
  1380. if (result == NULL) {
  1381. dev_err(&adap->dev, "of_i2c: Failure registering %s\n",
  1382. node->full_name);
  1383. of_node_put(node);
  1384. return ERR_PTR(-EINVAL);
  1385. }
  1386. return result;
  1387. }
  1388. static void of_i2c_register_devices(struct i2c_adapter *adap)
  1389. {
  1390. struct device_node *bus, *node;
  1391. struct i2c_client *client;
  1392. /* Only register child devices if the adapter has a node pointer set */
  1393. if (!adap->dev.of_node)
  1394. return;
  1395. dev_dbg(&adap->dev, "of_i2c: walking child nodes\n");
  1396. bus = of_get_child_by_name(adap->dev.of_node, "i2c-bus");
  1397. if (!bus)
  1398. bus = of_node_get(adap->dev.of_node);
  1399. for_each_available_child_of_node(bus, node) {
  1400. if (of_node_test_and_set_flag(node, OF_POPULATED))
  1401. continue;
  1402. client = of_i2c_register_device(adap, node);
  1403. if (IS_ERR(client)) {
  1404. dev_warn(&adap->dev,
  1405. "Failed to create I2C device for %s\n",
  1406. node->full_name);
  1407. of_node_clear_flag(node, OF_POPULATED);
  1408. }
  1409. }
  1410. of_node_put(bus);
  1411. }
  1412. static int of_dev_node_match(struct device *dev, void *data)
  1413. {
  1414. return dev->of_node == data;
  1415. }
  1416. /* must call put_device() when done with returned i2c_client device */
  1417. struct i2c_client *of_find_i2c_device_by_node(struct device_node *node)
  1418. {
  1419. struct device *dev;
  1420. struct i2c_client *client;
  1421. dev = bus_find_device(&i2c_bus_type, NULL, node, of_dev_node_match);
  1422. if (!dev)
  1423. return NULL;
  1424. client = i2c_verify_client(dev);
  1425. if (!client)
  1426. put_device(dev);
  1427. return client;
  1428. }
  1429. EXPORT_SYMBOL(of_find_i2c_device_by_node);
  1430. /* must call put_device() when done with returned i2c_adapter device */
  1431. struct i2c_adapter *of_find_i2c_adapter_by_node(struct device_node *node)
  1432. {
  1433. struct device *dev;
  1434. struct i2c_adapter *adapter;
  1435. dev = bus_find_device(&i2c_bus_type, NULL, node, of_dev_node_match);
  1436. if (!dev)
  1437. return NULL;
  1438. adapter = i2c_verify_adapter(dev);
  1439. if (!adapter)
  1440. put_device(dev);
  1441. return adapter;
  1442. }
  1443. EXPORT_SYMBOL(of_find_i2c_adapter_by_node);
  1444. /* must call i2c_put_adapter() when done with returned i2c_adapter device */
  1445. struct i2c_adapter *of_get_i2c_adapter_by_node(struct device_node *node)
  1446. {
  1447. struct i2c_adapter *adapter;
  1448. adapter = of_find_i2c_adapter_by_node(node);
  1449. if (!adapter)
  1450. return NULL;
  1451. if (!try_module_get(adapter->owner)) {
  1452. put_device(&adapter->dev);
  1453. adapter = NULL;
  1454. }
  1455. return adapter;
  1456. }
  1457. EXPORT_SYMBOL(of_get_i2c_adapter_by_node);
  1458. #else
  1459. static void of_i2c_register_devices(struct i2c_adapter *adap) { }
  1460. #endif /* CONFIG_OF */
  1461. static int i2c_do_add_adapter(struct i2c_driver *driver,
  1462. struct i2c_adapter *adap)
  1463. {
  1464. /* Detect supported devices on that bus, and instantiate them */
  1465. i2c_detect(adap, driver);
  1466. /* Let legacy drivers scan this bus for matching devices */
  1467. if (driver->attach_adapter) {
  1468. dev_warn(&adap->dev, "%s: attach_adapter method is deprecated\n",
  1469. driver->driver.name);
  1470. dev_warn(&adap->dev,
  1471. "Please use another way to instantiate your i2c_client\n");
  1472. /* We ignore the return code; if it fails, too bad */
  1473. driver->attach_adapter(adap);
  1474. }
  1475. return 0;
  1476. }
  1477. static int __process_new_adapter(struct device_driver *d, void *data)
  1478. {
  1479. return i2c_do_add_adapter(to_i2c_driver(d), data);
  1480. }
  1481. static const struct i2c_lock_operations i2c_adapter_lock_ops = {
  1482. .lock_bus = i2c_adapter_lock_bus,
  1483. .trylock_bus = i2c_adapter_trylock_bus,
  1484. .unlock_bus = i2c_adapter_unlock_bus,
  1485. };
  1486. static int i2c_register_adapter(struct i2c_adapter *adap)
  1487. {
  1488. int res = -EINVAL;
  1489. /* Can't register until after driver model init */
  1490. if (WARN_ON(!is_registered)) {
  1491. res = -EAGAIN;
  1492. goto out_list;
  1493. }
  1494. /* Sanity checks */
  1495. if (WARN(!adap->name[0], "i2c adapter has no name"))
  1496. goto out_list;
  1497. if (!adap->algo) {
  1498. pr_err("adapter '%s': no algo supplied!\n", adap->name);
  1499. goto out_list;
  1500. }
  1501. if (!adap->lock_ops)
  1502. adap->lock_ops = &i2c_adapter_lock_ops;
  1503. rt_mutex_init(&adap->bus_lock);
  1504. rt_mutex_init(&adap->mux_lock);
  1505. mutex_init(&adap->userspace_clients_lock);
  1506. INIT_LIST_HEAD(&adap->userspace_clients);
  1507. /* Set default timeout to 1 second if not already set */
  1508. if (adap->timeout == 0)
  1509. adap->timeout = HZ;
  1510. dev_set_name(&adap->dev, "i2c-%d", adap->nr);
  1511. adap->dev.bus = &i2c_bus_type;
  1512. adap->dev.type = &i2c_adapter_type;
  1513. res = device_register(&adap->dev);
  1514. if (res) {
  1515. pr_err("adapter '%s': can't register device (%d)\n", adap->name, res);
  1516. goto out_list;
  1517. }
  1518. dev_dbg(&adap->dev, "adapter [%s] registered\n", adap->name);
  1519. pm_runtime_no_callbacks(&adap->dev);
  1520. pm_suspend_ignore_children(&adap->dev, true);
  1521. pm_runtime_enable(&adap->dev);
  1522. #ifdef CONFIG_I2C_COMPAT
  1523. res = class_compat_create_link(i2c_adapter_compat_class, &adap->dev,
  1524. adap->dev.parent);
  1525. if (res)
  1526. dev_warn(&adap->dev,
  1527. "Failed to create compatibility class link\n");
  1528. #endif
  1529. i2c_init_recovery(adap);
  1530. /* create pre-declared device nodes */
  1531. of_i2c_register_devices(adap);
  1532. i2c_acpi_register_devices(adap);
  1533. i2c_acpi_install_space_handler(adap);
  1534. if (adap->nr < __i2c_first_dynamic_bus_num)
  1535. i2c_scan_static_board_info(adap);
  1536. /* Notify drivers */
  1537. mutex_lock(&core_lock);
  1538. bus_for_each_drv(&i2c_bus_type, NULL, adap, __process_new_adapter);
  1539. mutex_unlock(&core_lock);
  1540. return 0;
  1541. out_list:
  1542. mutex_lock(&core_lock);
  1543. idr_remove(&i2c_adapter_idr, adap->nr);
  1544. mutex_unlock(&core_lock);
  1545. return res;
  1546. }
  1547. /**
  1548. * __i2c_add_numbered_adapter - i2c_add_numbered_adapter where nr is never -1
  1549. * @adap: the adapter to register (with adap->nr initialized)
  1550. * Context: can sleep
  1551. *
  1552. * See i2c_add_numbered_adapter() for details.
  1553. */
  1554. static int __i2c_add_numbered_adapter(struct i2c_adapter *adap)
  1555. {
  1556. int id;
  1557. mutex_lock(&core_lock);
  1558. id = idr_alloc(&i2c_adapter_idr, adap, adap->nr, adap->nr + 1, GFP_KERNEL);
  1559. mutex_unlock(&core_lock);
  1560. if (WARN(id < 0, "couldn't get idr"))
  1561. return id == -ENOSPC ? -EBUSY : id;
  1562. return i2c_register_adapter(adap);
  1563. }
  1564. /**
  1565. * i2c_add_adapter - declare i2c adapter, use dynamic bus number
  1566. * @adapter: the adapter to add
  1567. * Context: can sleep
  1568. *
  1569. * This routine is used to declare an I2C adapter when its bus number
  1570. * doesn't matter or when its bus number is specified by an dt alias.
  1571. * Examples of bases when the bus number doesn't matter: I2C adapters
  1572. * dynamically added by USB links or PCI plugin cards.
  1573. *
  1574. * When this returns zero, a new bus number was allocated and stored
  1575. * in adap->nr, and the specified adapter became available for clients.
  1576. * Otherwise, a negative errno value is returned.
  1577. */
  1578. int i2c_add_adapter(struct i2c_adapter *adapter)
  1579. {
  1580. struct device *dev = &adapter->dev;
  1581. int id;
  1582. if (dev->of_node) {
  1583. id = of_alias_get_id(dev->of_node, "i2c");
  1584. if (id >= 0) {
  1585. adapter->nr = id;
  1586. return __i2c_add_numbered_adapter(adapter);
  1587. }
  1588. }
  1589. mutex_lock(&core_lock);
  1590. id = idr_alloc(&i2c_adapter_idr, adapter,
  1591. __i2c_first_dynamic_bus_num, 0, GFP_KERNEL);
  1592. mutex_unlock(&core_lock);
  1593. if (WARN(id < 0, "couldn't get idr"))
  1594. return id;
  1595. adapter->nr = id;
  1596. return i2c_register_adapter(adapter);
  1597. }
  1598. EXPORT_SYMBOL(i2c_add_adapter);
  1599. /**
  1600. * i2c_add_numbered_adapter - declare i2c adapter, use static bus number
  1601. * @adap: the adapter to register (with adap->nr initialized)
  1602. * Context: can sleep
  1603. *
  1604. * This routine is used to declare an I2C adapter when its bus number
  1605. * matters. For example, use it for I2C adapters from system-on-chip CPUs,
  1606. * or otherwise built in to the system's mainboard, and where i2c_board_info
  1607. * is used to properly configure I2C devices.
  1608. *
  1609. * If the requested bus number is set to -1, then this function will behave
  1610. * identically to i2c_add_adapter, and will dynamically assign a bus number.
  1611. *
  1612. * If no devices have pre-been declared for this bus, then be sure to
  1613. * register the adapter before any dynamically allocated ones. Otherwise
  1614. * the required bus ID may not be available.
  1615. *
  1616. * When this returns zero, the specified adapter became available for
  1617. * clients using the bus number provided in adap->nr. Also, the table
  1618. * of I2C devices pre-declared using i2c_register_board_info() is scanned,
  1619. * and the appropriate driver model device nodes are created. Otherwise, a
  1620. * negative errno value is returned.
  1621. */
  1622. int i2c_add_numbered_adapter(struct i2c_adapter *adap)
  1623. {
  1624. if (adap->nr == -1) /* -1 means dynamically assign bus id */
  1625. return i2c_add_adapter(adap);
  1626. return __i2c_add_numbered_adapter(adap);
  1627. }
  1628. EXPORT_SYMBOL_GPL(i2c_add_numbered_adapter);
  1629. static void i2c_do_del_adapter(struct i2c_driver *driver,
  1630. struct i2c_adapter *adapter)
  1631. {
  1632. struct i2c_client *client, *_n;
  1633. /* Remove the devices we created ourselves as the result of hardware
  1634. * probing (using a driver's detect method) */
  1635. list_for_each_entry_safe(client, _n, &driver->clients, detected) {
  1636. if (client->adapter == adapter) {
  1637. dev_dbg(&adapter->dev, "Removing %s at 0x%x\n",
  1638. client->name, client->addr);
  1639. list_del(&client->detected);
  1640. i2c_unregister_device(client);
  1641. }
  1642. }
  1643. }
  1644. static int __unregister_client(struct device *dev, void *dummy)
  1645. {
  1646. struct i2c_client *client = i2c_verify_client(dev);
  1647. if (client && strcmp(client->name, "dummy"))
  1648. i2c_unregister_device(client);
  1649. return 0;
  1650. }
  1651. static int __unregister_dummy(struct device *dev, void *dummy)
  1652. {
  1653. struct i2c_client *client = i2c_verify_client(dev);
  1654. if (client)
  1655. i2c_unregister_device(client);
  1656. return 0;
  1657. }
  1658. static int __process_removed_adapter(struct device_driver *d, void *data)
  1659. {
  1660. i2c_do_del_adapter(to_i2c_driver(d), data);
  1661. return 0;
  1662. }
  1663. /**
  1664. * i2c_del_adapter - unregister I2C adapter
  1665. * @adap: the adapter being unregistered
  1666. * Context: can sleep
  1667. *
  1668. * This unregisters an I2C adapter which was previously registered
  1669. * by @i2c_add_adapter or @i2c_add_numbered_adapter.
  1670. */
  1671. void i2c_del_adapter(struct i2c_adapter *adap)
  1672. {
  1673. struct i2c_adapter *found;
  1674. struct i2c_client *client, *next;
  1675. /* First make sure that this adapter was ever added */
  1676. mutex_lock(&core_lock);
  1677. found = idr_find(&i2c_adapter_idr, adap->nr);
  1678. mutex_unlock(&core_lock);
  1679. if (found != adap) {
  1680. pr_debug("attempting to delete unregistered adapter [%s]\n", adap->name);
  1681. return;
  1682. }
  1683. i2c_acpi_remove_space_handler(adap);
  1684. /* Tell drivers about this removal */
  1685. mutex_lock(&core_lock);
  1686. bus_for_each_drv(&i2c_bus_type, NULL, adap,
  1687. __process_removed_adapter);
  1688. mutex_unlock(&core_lock);
  1689. /* Remove devices instantiated from sysfs */
  1690. mutex_lock_nested(&adap->userspace_clients_lock,
  1691. i2c_adapter_depth(adap));
  1692. list_for_each_entry_safe(client, next, &adap->userspace_clients,
  1693. detected) {
  1694. dev_dbg(&adap->dev, "Removing %s at 0x%x\n", client->name,
  1695. client->addr);
  1696. list_del(&client->detected);
  1697. i2c_unregister_device(client);
  1698. }
  1699. mutex_unlock(&adap->userspace_clients_lock);
  1700. /* Detach any active clients. This can't fail, thus we do not
  1701. * check the returned value. This is a two-pass process, because
  1702. * we can't remove the dummy devices during the first pass: they
  1703. * could have been instantiated by real devices wishing to clean
  1704. * them up properly, so we give them a chance to do that first. */
  1705. device_for_each_child(&adap->dev, NULL, __unregister_client);
  1706. device_for_each_child(&adap->dev, NULL, __unregister_dummy);
  1707. #ifdef CONFIG_I2C_COMPAT
  1708. class_compat_remove_link(i2c_adapter_compat_class, &adap->dev,
  1709. adap->dev.parent);
  1710. #endif
  1711. /* device name is gone after device_unregister */
  1712. dev_dbg(&adap->dev, "adapter [%s] unregistered\n", adap->name);
  1713. pm_runtime_disable(&adap->dev);
  1714. /* wait until all references to the device are gone
  1715. *
  1716. * FIXME: This is old code and should ideally be replaced by an
  1717. * alternative which results in decoupling the lifetime of the struct
  1718. * device from the i2c_adapter, like spi or netdev do. Any solution
  1719. * should be thoroughly tested with DEBUG_KOBJECT_RELEASE enabled!
  1720. */
  1721. init_completion(&adap->dev_released);
  1722. device_unregister(&adap->dev);
  1723. wait_for_completion(&adap->dev_released);
  1724. /* free bus id */
  1725. mutex_lock(&core_lock);
  1726. idr_remove(&i2c_adapter_idr, adap->nr);
  1727. mutex_unlock(&core_lock);
  1728. /* Clear the device structure in case this adapter is ever going to be
  1729. added again */
  1730. memset(&adap->dev, 0, sizeof(adap->dev));
  1731. }
  1732. EXPORT_SYMBOL(i2c_del_adapter);
  1733. /**
  1734. * i2c_parse_fw_timings - get I2C related timing parameters from firmware
  1735. * @dev: The device to scan for I2C timing properties
  1736. * @t: the i2c_timings struct to be filled with values
  1737. * @use_defaults: bool to use sane defaults derived from the I2C specification
  1738. * when properties are not found, otherwise use 0
  1739. *
  1740. * Scan the device for the generic I2C properties describing timing parameters
  1741. * for the signal and fill the given struct with the results. If a property was
  1742. * not found and use_defaults was true, then maximum timings are assumed which
  1743. * are derived from the I2C specification. If use_defaults is not used, the
  1744. * results will be 0, so drivers can apply their own defaults later. The latter
  1745. * is mainly intended for avoiding regressions of existing drivers which want
  1746. * to switch to this function. New drivers almost always should use the defaults.
  1747. */
  1748. void i2c_parse_fw_timings(struct device *dev, struct i2c_timings *t, bool use_defaults)
  1749. {
  1750. int ret;
  1751. memset(t, 0, sizeof(*t));
  1752. ret = device_property_read_u32(dev, "clock-frequency", &t->bus_freq_hz);
  1753. if (ret && use_defaults)
  1754. t->bus_freq_hz = 100000;
  1755. ret = device_property_read_u32(dev, "i2c-scl-rising-time-ns", &t->scl_rise_ns);
  1756. if (ret && use_defaults) {
  1757. if (t->bus_freq_hz <= 100000)
  1758. t->scl_rise_ns = 1000;
  1759. else if (t->bus_freq_hz <= 400000)
  1760. t->scl_rise_ns = 300;
  1761. else
  1762. t->scl_rise_ns = 120;
  1763. }
  1764. ret = device_property_read_u32(dev, "i2c-scl-falling-time-ns", &t->scl_fall_ns);
  1765. if (ret && use_defaults) {
  1766. if (t->bus_freq_hz <= 400000)
  1767. t->scl_fall_ns = 300;
  1768. else
  1769. t->scl_fall_ns = 120;
  1770. }
  1771. device_property_read_u32(dev, "i2c-scl-internal-delay-ns", &t->scl_int_delay_ns);
  1772. ret = device_property_read_u32(dev, "i2c-sda-falling-time-ns", &t->sda_fall_ns);
  1773. if (ret && use_defaults)
  1774. t->sda_fall_ns = t->scl_fall_ns;
  1775. }
  1776. EXPORT_SYMBOL_GPL(i2c_parse_fw_timings);
  1777. /* ------------------------------------------------------------------------- */
  1778. int i2c_for_each_dev(void *data, int (*fn)(struct device *, void *))
  1779. {
  1780. int res;
  1781. mutex_lock(&core_lock);
  1782. res = bus_for_each_dev(&i2c_bus_type, NULL, data, fn);
  1783. mutex_unlock(&core_lock);
  1784. return res;
  1785. }
  1786. EXPORT_SYMBOL_GPL(i2c_for_each_dev);
  1787. static int __process_new_driver(struct device *dev, void *data)
  1788. {
  1789. if (dev->type != &i2c_adapter_type)
  1790. return 0;
  1791. return i2c_do_add_adapter(data, to_i2c_adapter(dev));
  1792. }
  1793. /*
  1794. * An i2c_driver is used with one or more i2c_client (device) nodes to access
  1795. * i2c slave chips, on a bus instance associated with some i2c_adapter.
  1796. */
  1797. int i2c_register_driver(struct module *owner, struct i2c_driver *driver)
  1798. {
  1799. int res;
  1800. /* Can't register until after driver model init */
  1801. if (WARN_ON(!is_registered))
  1802. return -EAGAIN;
  1803. /* add the driver to the list of i2c drivers in the driver core */
  1804. driver->driver.owner = owner;
  1805. driver->driver.bus = &i2c_bus_type;
  1806. INIT_LIST_HEAD(&driver->clients);
  1807. /* When registration returns, the driver core
  1808. * will have called probe() for all matching-but-unbound devices.
  1809. */
  1810. res = driver_register(&driver->driver);
  1811. if (res)
  1812. return res;
  1813. pr_debug("driver [%s] registered\n", driver->driver.name);
  1814. /* Walk the adapters that are already present */
  1815. i2c_for_each_dev(driver, __process_new_driver);
  1816. return 0;
  1817. }
  1818. EXPORT_SYMBOL(i2c_register_driver);
  1819. static int __process_removed_driver(struct device *dev, void *data)
  1820. {
  1821. if (dev->type == &i2c_adapter_type)
  1822. i2c_do_del_adapter(data, to_i2c_adapter(dev));
  1823. return 0;
  1824. }
  1825. /**
  1826. * i2c_del_driver - unregister I2C driver
  1827. * @driver: the driver being unregistered
  1828. * Context: can sleep
  1829. */
  1830. void i2c_del_driver(struct i2c_driver *driver)
  1831. {
  1832. i2c_for_each_dev(driver, __process_removed_driver);
  1833. driver_unregister(&driver->driver);
  1834. pr_debug("driver [%s] unregistered\n", driver->driver.name);
  1835. }
  1836. EXPORT_SYMBOL(i2c_del_driver);
  1837. /* ------------------------------------------------------------------------- */
  1838. /**
  1839. * i2c_use_client - increments the reference count of the i2c client structure
  1840. * @client: the client being referenced
  1841. *
  1842. * Each live reference to a client should be refcounted. The driver model does
  1843. * that automatically as part of driver binding, so that most drivers don't
  1844. * need to do this explicitly: they hold a reference until they're unbound
  1845. * from the device.
  1846. *
  1847. * A pointer to the client with the incremented reference counter is returned.
  1848. */
  1849. struct i2c_client *i2c_use_client(struct i2c_client *client)
  1850. {
  1851. if (client && get_device(&client->dev))
  1852. return client;
  1853. return NULL;
  1854. }
  1855. EXPORT_SYMBOL(i2c_use_client);
  1856. /**
  1857. * i2c_release_client - release a use of the i2c client structure
  1858. * @client: the client being no longer referenced
  1859. *
  1860. * Must be called when a user of a client is finished with it.
  1861. */
  1862. void i2c_release_client(struct i2c_client *client)
  1863. {
  1864. if (client)
  1865. put_device(&client->dev);
  1866. }
  1867. EXPORT_SYMBOL(i2c_release_client);
  1868. struct i2c_cmd_arg {
  1869. unsigned cmd;
  1870. void *arg;
  1871. };
  1872. static int i2c_cmd(struct device *dev, void *_arg)
  1873. {
  1874. struct i2c_client *client = i2c_verify_client(dev);
  1875. struct i2c_cmd_arg *arg = _arg;
  1876. struct i2c_driver *driver;
  1877. if (!client || !client->dev.driver)
  1878. return 0;
  1879. driver = to_i2c_driver(client->dev.driver);
  1880. if (driver->command)
  1881. driver->command(client, arg->cmd, arg->arg);
  1882. return 0;
  1883. }
  1884. void i2c_clients_command(struct i2c_adapter *adap, unsigned int cmd, void *arg)
  1885. {
  1886. struct i2c_cmd_arg cmd_arg;
  1887. cmd_arg.cmd = cmd;
  1888. cmd_arg.arg = arg;
  1889. device_for_each_child(&adap->dev, &cmd_arg, i2c_cmd);
  1890. }
  1891. EXPORT_SYMBOL(i2c_clients_command);
  1892. #if IS_ENABLED(CONFIG_OF_DYNAMIC)
  1893. static int of_i2c_notify(struct notifier_block *nb, unsigned long action,
  1894. void *arg)
  1895. {
  1896. struct of_reconfig_data *rd = arg;
  1897. struct i2c_adapter *adap;
  1898. struct i2c_client *client;
  1899. switch (of_reconfig_get_state_change(action, rd)) {
  1900. case OF_RECONFIG_CHANGE_ADD:
  1901. adap = of_find_i2c_adapter_by_node(rd->dn->parent);
  1902. if (adap == NULL)
  1903. return NOTIFY_OK; /* not for us */
  1904. if (of_node_test_and_set_flag(rd->dn, OF_POPULATED)) {
  1905. put_device(&adap->dev);
  1906. return NOTIFY_OK;
  1907. }
  1908. client = of_i2c_register_device(adap, rd->dn);
  1909. put_device(&adap->dev);
  1910. if (IS_ERR(client)) {
  1911. dev_err(&adap->dev, "failed to create client for '%s'\n",
  1912. rd->dn->full_name);
  1913. of_node_clear_flag(rd->dn, OF_POPULATED);
  1914. return notifier_from_errno(PTR_ERR(client));
  1915. }
  1916. break;
  1917. case OF_RECONFIG_CHANGE_REMOVE:
  1918. /* already depopulated? */
  1919. if (!of_node_check_flag(rd->dn, OF_POPULATED))
  1920. return NOTIFY_OK;
  1921. /* find our device by node */
  1922. client = of_find_i2c_device_by_node(rd->dn);
  1923. if (client == NULL)
  1924. return NOTIFY_OK; /* no? not meant for us */
  1925. /* unregister takes one ref away */
  1926. i2c_unregister_device(client);
  1927. /* and put the reference of the find */
  1928. put_device(&client->dev);
  1929. break;
  1930. }
  1931. return NOTIFY_OK;
  1932. }
  1933. static struct notifier_block i2c_of_notifier = {
  1934. .notifier_call = of_i2c_notify,
  1935. };
  1936. #else
  1937. extern struct notifier_block i2c_of_notifier;
  1938. #endif /* CONFIG_OF_DYNAMIC */
  1939. static int __init i2c_init(void)
  1940. {
  1941. int retval;
  1942. retval = of_alias_get_highest_id("i2c");
  1943. down_write(&__i2c_board_lock);
  1944. if (retval >= __i2c_first_dynamic_bus_num)
  1945. __i2c_first_dynamic_bus_num = retval + 1;
  1946. up_write(&__i2c_board_lock);
  1947. retval = bus_register(&i2c_bus_type);
  1948. if (retval)
  1949. return retval;
  1950. is_registered = true;
  1951. #ifdef CONFIG_I2C_COMPAT
  1952. i2c_adapter_compat_class = class_compat_register("i2c-adapter");
  1953. if (!i2c_adapter_compat_class) {
  1954. retval = -ENOMEM;
  1955. goto bus_err;
  1956. }
  1957. #endif
  1958. retval = i2c_add_driver(&dummy_driver);
  1959. if (retval)
  1960. goto class_err;
  1961. if (IS_ENABLED(CONFIG_OF_DYNAMIC))
  1962. WARN_ON(of_reconfig_notifier_register(&i2c_of_notifier));
  1963. if (IS_ENABLED(CONFIG_ACPI))
  1964. WARN_ON(acpi_reconfig_notifier_register(&i2c_acpi_notifier));
  1965. return 0;
  1966. class_err:
  1967. #ifdef CONFIG_I2C_COMPAT
  1968. class_compat_unregister(i2c_adapter_compat_class);
  1969. bus_err:
  1970. #endif
  1971. is_registered = false;
  1972. bus_unregister(&i2c_bus_type);
  1973. return retval;
  1974. }
  1975. static void __exit i2c_exit(void)
  1976. {
  1977. if (IS_ENABLED(CONFIG_ACPI))
  1978. WARN_ON(acpi_reconfig_notifier_unregister(&i2c_acpi_notifier));
  1979. if (IS_ENABLED(CONFIG_OF_DYNAMIC))
  1980. WARN_ON(of_reconfig_notifier_unregister(&i2c_of_notifier));
  1981. i2c_del_driver(&dummy_driver);
  1982. #ifdef CONFIG_I2C_COMPAT
  1983. class_compat_unregister(i2c_adapter_compat_class);
  1984. #endif
  1985. bus_unregister(&i2c_bus_type);
  1986. tracepoint_synchronize_unregister();
  1987. }
  1988. /* We must initialize early, because some subsystems register i2c drivers
  1989. * in subsys_initcall() code, but are linked (and initialized) before i2c.
  1990. */
  1991. postcore_initcall(i2c_init);
  1992. module_exit(i2c_exit);
  1993. /* ----------------------------------------------------
  1994. * the functional interface to the i2c busses.
  1995. * ----------------------------------------------------
  1996. */
  1997. /* Check if val is exceeding the quirk IFF quirk is non 0 */
  1998. #define i2c_quirk_exceeded(val, quirk) ((quirk) && ((val) > (quirk)))
  1999. static int i2c_quirk_error(struct i2c_adapter *adap, struct i2c_msg *msg, char *err_msg)
  2000. {
  2001. dev_err_ratelimited(&adap->dev, "adapter quirk: %s (addr 0x%04x, size %u, %s)\n",
  2002. err_msg, msg->addr, msg->len,
  2003. msg->flags & I2C_M_RD ? "read" : "write");
  2004. return -EOPNOTSUPP;
  2005. }
  2006. static int i2c_check_for_quirks(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
  2007. {
  2008. const struct i2c_adapter_quirks *q = adap->quirks;
  2009. int max_num = q->max_num_msgs, i;
  2010. bool do_len_check = true;
  2011. if (q->flags & I2C_AQ_COMB) {
  2012. max_num = 2;
  2013. /* special checks for combined messages */
  2014. if (num == 2) {
  2015. if (q->flags & I2C_AQ_COMB_WRITE_FIRST && msgs[0].flags & I2C_M_RD)
  2016. return i2c_quirk_error(adap, &msgs[0], "1st comb msg must be write");
  2017. if (q->flags & I2C_AQ_COMB_READ_SECOND && !(msgs[1].flags & I2C_M_RD))
  2018. return i2c_quirk_error(adap, &msgs[1], "2nd comb msg must be read");
  2019. if (q->flags & I2C_AQ_COMB_SAME_ADDR && msgs[0].addr != msgs[1].addr)
  2020. return i2c_quirk_error(adap, &msgs[0], "comb msg only to same addr");
  2021. if (i2c_quirk_exceeded(msgs[0].len, q->max_comb_1st_msg_len))
  2022. return i2c_quirk_error(adap, &msgs[0], "msg too long");
  2023. if (i2c_quirk_exceeded(msgs[1].len, q->max_comb_2nd_msg_len))
  2024. return i2c_quirk_error(adap, &msgs[1], "msg too long");
  2025. do_len_check = false;
  2026. }
  2027. }
  2028. if (i2c_quirk_exceeded(num, max_num))
  2029. return i2c_quirk_error(adap, &msgs[0], "too many messages");
  2030. for (i = 0; i < num; i++) {
  2031. u16 len = msgs[i].len;
  2032. if (msgs[i].flags & I2C_M_RD) {
  2033. if (do_len_check && i2c_quirk_exceeded(len, q->max_read_len))
  2034. return i2c_quirk_error(adap, &msgs[i], "msg too long");
  2035. } else {
  2036. if (do_len_check && i2c_quirk_exceeded(len, q->max_write_len))
  2037. return i2c_quirk_error(adap, &msgs[i], "msg too long");
  2038. }
  2039. }
  2040. return 0;
  2041. }
  2042. /**
  2043. * __i2c_transfer - unlocked flavor of i2c_transfer
  2044. * @adap: Handle to I2C bus
  2045. * @msgs: One or more messages to execute before STOP is issued to
  2046. * terminate the operation; each message begins with a START.
  2047. * @num: Number of messages to be executed.
  2048. *
  2049. * Returns negative errno, else the number of messages executed.
  2050. *
  2051. * Adapter lock must be held when calling this function. No debug logging
  2052. * takes place. adap->algo->master_xfer existence isn't checked.
  2053. */
  2054. int __i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
  2055. {
  2056. unsigned long orig_jiffies;
  2057. int ret, try;
  2058. if (adap->quirks && i2c_check_for_quirks(adap, msgs, num))
  2059. return -EOPNOTSUPP;
  2060. /* i2c_trace_msg gets enabled when tracepoint i2c_transfer gets
  2061. * enabled. This is an efficient way of keeping the for-loop from
  2062. * being executed when not needed.
  2063. */
  2064. if (static_key_false(&i2c_trace_msg)) {
  2065. int i;
  2066. for (i = 0; i < num; i++)
  2067. if (msgs[i].flags & I2C_M_RD)
  2068. trace_i2c_read(adap, &msgs[i], i);
  2069. else
  2070. trace_i2c_write(adap, &msgs[i], i);
  2071. }
  2072. /* Retry automatically on arbitration loss */
  2073. orig_jiffies = jiffies;
  2074. for (ret = 0, try = 0; try <= adap->retries; try++) {
  2075. ret = adap->algo->master_xfer(adap, msgs, num);
  2076. if (ret != -EAGAIN)
  2077. break;
  2078. if (time_after(jiffies, orig_jiffies + adap->timeout))
  2079. break;
  2080. }
  2081. if (static_key_false(&i2c_trace_msg)) {
  2082. int i;
  2083. for (i = 0; i < ret; i++)
  2084. if (msgs[i].flags & I2C_M_RD)
  2085. trace_i2c_reply(adap, &msgs[i], i);
  2086. trace_i2c_result(adap, i, ret);
  2087. }
  2088. return ret;
  2089. }
  2090. EXPORT_SYMBOL(__i2c_transfer);
  2091. /**
  2092. * i2c_transfer - execute a single or combined I2C message
  2093. * @adap: Handle to I2C bus
  2094. * @msgs: One or more messages to execute before STOP is issued to
  2095. * terminate the operation; each message begins with a START.
  2096. * @num: Number of messages to be executed.
  2097. *
  2098. * Returns negative errno, else the number of messages executed.
  2099. *
  2100. * Note that there is no requirement that each message be sent to
  2101. * the same slave address, although that is the most common model.
  2102. */
  2103. int i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
  2104. {
  2105. int ret;
  2106. /* REVISIT the fault reporting model here is weak:
  2107. *
  2108. * - When we get an error after receiving N bytes from a slave,
  2109. * there is no way to report "N".
  2110. *
  2111. * - When we get a NAK after transmitting N bytes to a slave,
  2112. * there is no way to report "N" ... or to let the master
  2113. * continue executing the rest of this combined message, if
  2114. * that's the appropriate response.
  2115. *
  2116. * - When for example "num" is two and we successfully complete
  2117. * the first message but get an error part way through the
  2118. * second, it's unclear whether that should be reported as
  2119. * one (discarding status on the second message) or errno
  2120. * (discarding status on the first one).
  2121. */
  2122. if (adap->algo->master_xfer) {
  2123. #ifdef DEBUG
  2124. for (ret = 0; ret < num; ret++) {
  2125. dev_dbg(&adap->dev,
  2126. "master_xfer[%d] %c, addr=0x%02x, len=%d%s\n",
  2127. ret, (msgs[ret].flags & I2C_M_RD) ? 'R' : 'W',
  2128. msgs[ret].addr, msgs[ret].len,
  2129. (msgs[ret].flags & I2C_M_RECV_LEN) ? "+" : "");
  2130. }
  2131. #endif
  2132. if (in_atomic() || irqs_disabled()) {
  2133. ret = i2c_trylock_bus(adap, I2C_LOCK_SEGMENT);
  2134. if (!ret)
  2135. /* I2C activity is ongoing. */
  2136. return -EAGAIN;
  2137. } else {
  2138. i2c_lock_bus(adap, I2C_LOCK_SEGMENT);
  2139. }
  2140. ret = __i2c_transfer(adap, msgs, num);
  2141. i2c_unlock_bus(adap, I2C_LOCK_SEGMENT);
  2142. return ret;
  2143. } else {
  2144. dev_dbg(&adap->dev, "I2C level transfers not supported\n");
  2145. return -EOPNOTSUPP;
  2146. }
  2147. }
  2148. EXPORT_SYMBOL(i2c_transfer);
  2149. /**
  2150. * i2c_master_send - issue a single I2C message in master transmit mode
  2151. * @client: Handle to slave device
  2152. * @buf: Data that will be written to the slave
  2153. * @count: How many bytes to write, must be less than 64k since msg.len is u16
  2154. *
  2155. * Returns negative errno, or else the number of bytes written.
  2156. */
  2157. int i2c_master_send(const struct i2c_client *client, const char *buf, int count)
  2158. {
  2159. int ret;
  2160. struct i2c_adapter *adap = client->adapter;
  2161. struct i2c_msg msg;
  2162. msg.addr = client->addr;
  2163. msg.flags = client->flags & I2C_M_TEN;
  2164. msg.len = count;
  2165. msg.buf = (char *)buf;
  2166. ret = i2c_transfer(adap, &msg, 1);
  2167. /*
  2168. * If everything went ok (i.e. 1 msg transmitted), return #bytes
  2169. * transmitted, else error code.
  2170. */
  2171. return (ret == 1) ? count : ret;
  2172. }
  2173. EXPORT_SYMBOL(i2c_master_send);
  2174. /**
  2175. * i2c_master_recv - issue a single I2C message in master receive mode
  2176. * @client: Handle to slave device
  2177. * @buf: Where to store data read from slave
  2178. * @count: How many bytes to read, must be less than 64k since msg.len is u16
  2179. *
  2180. * Returns negative errno, or else the number of bytes read.
  2181. */
  2182. int i2c_master_recv(const struct i2c_client *client, char *buf, int count)
  2183. {
  2184. struct i2c_adapter *adap = client->adapter;
  2185. struct i2c_msg msg;
  2186. int ret;
  2187. msg.addr = client->addr;
  2188. msg.flags = client->flags & I2C_M_TEN;
  2189. msg.flags |= I2C_M_RD;
  2190. msg.len = count;
  2191. msg.buf = buf;
  2192. ret = i2c_transfer(adap, &msg, 1);
  2193. /*
  2194. * If everything went ok (i.e. 1 msg received), return #bytes received,
  2195. * else error code.
  2196. */
  2197. return (ret == 1) ? count : ret;
  2198. }
  2199. EXPORT_SYMBOL(i2c_master_recv);
  2200. /* ----------------------------------------------------
  2201. * the i2c address scanning function
  2202. * Will not work for 10-bit addresses!
  2203. * ----------------------------------------------------
  2204. */
  2205. /*
  2206. * Legacy default probe function, mostly relevant for SMBus. The default
  2207. * probe method is a quick write, but it is known to corrupt the 24RF08
  2208. * EEPROMs due to a state machine bug, and could also irreversibly
  2209. * write-protect some EEPROMs, so for address ranges 0x30-0x37 and 0x50-0x5f,
  2210. * we use a short byte read instead. Also, some bus drivers don't implement
  2211. * quick write, so we fallback to a byte read in that case too.
  2212. * On x86, there is another special case for FSC hardware monitoring chips,
  2213. * which want regular byte reads (address 0x73.) Fortunately, these are the
  2214. * only known chips using this I2C address on PC hardware.
  2215. * Returns 1 if probe succeeded, 0 if not.
  2216. */
  2217. static int i2c_default_probe(struct i2c_adapter *adap, unsigned short addr)
  2218. {
  2219. int err;
  2220. union i2c_smbus_data dummy;
  2221. #ifdef CONFIG_X86
  2222. if (addr == 0x73 && (adap->class & I2C_CLASS_HWMON)
  2223. && i2c_check_functionality(adap, I2C_FUNC_SMBUS_READ_BYTE_DATA))
  2224. err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0,
  2225. I2C_SMBUS_BYTE_DATA, &dummy);
  2226. else
  2227. #endif
  2228. if (!((addr & ~0x07) == 0x30 || (addr & ~0x0f) == 0x50)
  2229. && i2c_check_functionality(adap, I2C_FUNC_SMBUS_QUICK))
  2230. err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_WRITE, 0,
  2231. I2C_SMBUS_QUICK, NULL);
  2232. else if (i2c_check_functionality(adap, I2C_FUNC_SMBUS_READ_BYTE))
  2233. err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0,
  2234. I2C_SMBUS_BYTE, &dummy);
  2235. else {
  2236. dev_warn(&adap->dev, "No suitable probing method supported for address 0x%02X\n",
  2237. addr);
  2238. err = -EOPNOTSUPP;
  2239. }
  2240. return err >= 0;
  2241. }
  2242. static int i2c_detect_address(struct i2c_client *temp_client,
  2243. struct i2c_driver *driver)
  2244. {
  2245. struct i2c_board_info info;
  2246. struct i2c_adapter *adapter = temp_client->adapter;
  2247. int addr = temp_client->addr;
  2248. int err;
  2249. /* Make sure the address is valid */
  2250. err = i2c_check_7bit_addr_validity_strict(addr);
  2251. if (err) {
  2252. dev_warn(&adapter->dev, "Invalid probe address 0x%02x\n",
  2253. addr);
  2254. return err;
  2255. }
  2256. /* Skip if already in use (7 bit, no need to encode flags) */
  2257. if (i2c_check_addr_busy(adapter, addr))
  2258. return 0;
  2259. /* Make sure there is something at this address */
  2260. if (!i2c_default_probe(adapter, addr))
  2261. return 0;
  2262. /* Finally call the custom detection function */
  2263. memset(&info, 0, sizeof(struct i2c_board_info));
  2264. info.addr = addr;
  2265. err = driver->detect(temp_client, &info);
  2266. if (err) {
  2267. /* -ENODEV is returned if the detection fails. We catch it
  2268. here as this isn't an error. */
  2269. return err == -ENODEV ? 0 : err;
  2270. }
  2271. /* Consistency check */
  2272. if (info.type[0] == '\0') {
  2273. dev_err(&adapter->dev,
  2274. "%s detection function provided no name for 0x%x\n",
  2275. driver->driver.name, addr);
  2276. } else {
  2277. struct i2c_client *client;
  2278. /* Detection succeeded, instantiate the device */
  2279. if (adapter->class & I2C_CLASS_DEPRECATED)
  2280. dev_warn(&adapter->dev,
  2281. "This adapter will soon drop class based instantiation of devices. "
  2282. "Please make sure client 0x%02x gets instantiated by other means. "
  2283. "Check 'Documentation/i2c/instantiating-devices' for details.\n",
  2284. info.addr);
  2285. dev_dbg(&adapter->dev, "Creating %s at 0x%02x\n",
  2286. info.type, info.addr);
  2287. client = i2c_new_device(adapter, &info);
  2288. if (client)
  2289. list_add_tail(&client->detected, &driver->clients);
  2290. else
  2291. dev_err(&adapter->dev, "Failed creating %s at 0x%02x\n",
  2292. info.type, info.addr);
  2293. }
  2294. return 0;
  2295. }
  2296. static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver)
  2297. {
  2298. const unsigned short *address_list;
  2299. struct i2c_client *temp_client;
  2300. int i, err = 0;
  2301. int adap_id = i2c_adapter_id(adapter);
  2302. address_list = driver->address_list;
  2303. if (!driver->detect || !address_list)
  2304. return 0;
  2305. /* Warn that the adapter lost class based instantiation */
  2306. if (adapter->class == I2C_CLASS_DEPRECATED) {
  2307. dev_dbg(&adapter->dev,
  2308. "This adapter dropped support for I2C classes and won't auto-detect %s devices anymore. "
  2309. "If you need it, check 'Documentation/i2c/instantiating-devices' for alternatives.\n",
  2310. driver->driver.name);
  2311. return 0;
  2312. }
  2313. /* Stop here if the classes do not match */
  2314. if (!(adapter->class & driver->class))
  2315. return 0;
  2316. /* Set up a temporary client to help detect callback */
  2317. temp_client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
  2318. if (!temp_client)
  2319. return -ENOMEM;
  2320. temp_client->adapter = adapter;
  2321. for (i = 0; address_list[i] != I2C_CLIENT_END; i += 1) {
  2322. dev_dbg(&adapter->dev,
  2323. "found normal entry for adapter %d, addr 0x%02x\n",
  2324. adap_id, address_list[i]);
  2325. temp_client->addr = address_list[i];
  2326. err = i2c_detect_address(temp_client, driver);
  2327. if (unlikely(err))
  2328. break;
  2329. }
  2330. kfree(temp_client);
  2331. return err;
  2332. }
  2333. int i2c_probe_func_quick_read(struct i2c_adapter *adap, unsigned short addr)
  2334. {
  2335. return i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0,
  2336. I2C_SMBUS_QUICK, NULL) >= 0;
  2337. }
  2338. EXPORT_SYMBOL_GPL(i2c_probe_func_quick_read);
  2339. struct i2c_client *
  2340. i2c_new_probed_device(struct i2c_adapter *adap,
  2341. struct i2c_board_info *info,
  2342. unsigned short const *addr_list,
  2343. int (*probe)(struct i2c_adapter *, unsigned short addr))
  2344. {
  2345. int i;
  2346. if (!probe)
  2347. probe = i2c_default_probe;
  2348. for (i = 0; addr_list[i] != I2C_CLIENT_END; i++) {
  2349. /* Check address validity */
  2350. if (i2c_check_7bit_addr_validity_strict(addr_list[i]) < 0) {
  2351. dev_warn(&adap->dev, "Invalid 7-bit address 0x%02x\n",
  2352. addr_list[i]);
  2353. continue;
  2354. }
  2355. /* Check address availability (7 bit, no need to encode flags) */
  2356. if (i2c_check_addr_busy(adap, addr_list[i])) {
  2357. dev_dbg(&adap->dev,
  2358. "Address 0x%02x already in use, not probing\n",
  2359. addr_list[i]);
  2360. continue;
  2361. }
  2362. /* Test address responsiveness */
  2363. if (probe(adap, addr_list[i]))
  2364. break;
  2365. }
  2366. if (addr_list[i] == I2C_CLIENT_END) {
  2367. dev_dbg(&adap->dev, "Probing failed, no device found\n");
  2368. return NULL;
  2369. }
  2370. info->addr = addr_list[i];
  2371. return i2c_new_device(adap, info);
  2372. }
  2373. EXPORT_SYMBOL_GPL(i2c_new_probed_device);
  2374. struct i2c_adapter *i2c_get_adapter(int nr)
  2375. {
  2376. struct i2c_adapter *adapter;
  2377. mutex_lock(&core_lock);
  2378. adapter = idr_find(&i2c_adapter_idr, nr);
  2379. if (!adapter)
  2380. goto exit;
  2381. if (try_module_get(adapter->owner))
  2382. get_device(&adapter->dev);
  2383. else
  2384. adapter = NULL;
  2385. exit:
  2386. mutex_unlock(&core_lock);
  2387. return adapter;
  2388. }
  2389. EXPORT_SYMBOL(i2c_get_adapter);
  2390. void i2c_put_adapter(struct i2c_adapter *adap)
  2391. {
  2392. if (!adap)
  2393. return;
  2394. put_device(&adap->dev);
  2395. module_put(adap->owner);
  2396. }
  2397. EXPORT_SYMBOL(i2c_put_adapter);
  2398. /* The SMBus parts */
  2399. #define POLY (0x1070U << 3)
  2400. static u8 crc8(u16 data)
  2401. {
  2402. int i;
  2403. for (i = 0; i < 8; i++) {
  2404. if (data & 0x8000)
  2405. data = data ^ POLY;
  2406. data = data << 1;
  2407. }
  2408. return (u8)(data >> 8);
  2409. }
  2410. /* Incremental CRC8 over count bytes in the array pointed to by p */
  2411. static u8 i2c_smbus_pec(u8 crc, u8 *p, size_t count)
  2412. {
  2413. int i;
  2414. for (i = 0; i < count; i++)
  2415. crc = crc8((crc ^ p[i]) << 8);
  2416. return crc;
  2417. }
  2418. /* Assume a 7-bit address, which is reasonable for SMBus */
  2419. static u8 i2c_smbus_msg_pec(u8 pec, struct i2c_msg *msg)
  2420. {
  2421. /* The address will be sent first */
  2422. u8 addr = i2c_8bit_addr_from_msg(msg);
  2423. pec = i2c_smbus_pec(pec, &addr, 1);
  2424. /* The data buffer follows */
  2425. return i2c_smbus_pec(pec, msg->buf, msg->len);
  2426. }
  2427. /* Used for write only transactions */
  2428. static inline void i2c_smbus_add_pec(struct i2c_msg *msg)
  2429. {
  2430. msg->buf[msg->len] = i2c_smbus_msg_pec(0, msg);
  2431. msg->len++;
  2432. }
  2433. /* Return <0 on CRC error
  2434. If there was a write before this read (most cases) we need to take the
  2435. partial CRC from the write part into account.
  2436. Note that this function does modify the message (we need to decrease the
  2437. message length to hide the CRC byte from the caller). */
  2438. static int i2c_smbus_check_pec(u8 cpec, struct i2c_msg *msg)
  2439. {
  2440. u8 rpec = msg->buf[--msg->len];
  2441. cpec = i2c_smbus_msg_pec(cpec, msg);
  2442. if (rpec != cpec) {
  2443. pr_debug("Bad PEC 0x%02x vs. 0x%02x\n",
  2444. rpec, cpec);
  2445. return -EBADMSG;
  2446. }
  2447. return 0;
  2448. }
  2449. /**
  2450. * i2c_smbus_read_byte - SMBus "receive byte" protocol
  2451. * @client: Handle to slave device
  2452. *
  2453. * This executes the SMBus "receive byte" protocol, returning negative errno
  2454. * else the byte received from the device.
  2455. */
  2456. s32 i2c_smbus_read_byte(const struct i2c_client *client)
  2457. {
  2458. union i2c_smbus_data data;
  2459. int status;
  2460. status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
  2461. I2C_SMBUS_READ, 0,
  2462. I2C_SMBUS_BYTE, &data);
  2463. return (status < 0) ? status : data.byte;
  2464. }
  2465. EXPORT_SYMBOL(i2c_smbus_read_byte);
  2466. /**
  2467. * i2c_smbus_write_byte - SMBus "send byte" protocol
  2468. * @client: Handle to slave device
  2469. * @value: Byte to be sent
  2470. *
  2471. * This executes the SMBus "send byte" protocol, returning negative errno
  2472. * else zero on success.
  2473. */
  2474. s32 i2c_smbus_write_byte(const struct i2c_client *client, u8 value)
  2475. {
  2476. return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
  2477. I2C_SMBUS_WRITE, value, I2C_SMBUS_BYTE, NULL);
  2478. }
  2479. EXPORT_SYMBOL(i2c_smbus_write_byte);
  2480. /**
  2481. * i2c_smbus_read_byte_data - SMBus "read byte" protocol
  2482. * @client: Handle to slave device
  2483. * @command: Byte interpreted by slave
  2484. *
  2485. * This executes the SMBus "read byte" protocol, returning negative errno
  2486. * else a data byte received from the device.
  2487. */
  2488. s32 i2c_smbus_read_byte_data(const struct i2c_client *client, u8 command)
  2489. {
  2490. union i2c_smbus_data data;
  2491. int status;
  2492. status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
  2493. I2C_SMBUS_READ, command,
  2494. I2C_SMBUS_BYTE_DATA, &data);
  2495. return (status < 0) ? status : data.byte;
  2496. }
  2497. EXPORT_SYMBOL(i2c_smbus_read_byte_data);
  2498. /**
  2499. * i2c_smbus_write_byte_data - SMBus "write byte" protocol
  2500. * @client: Handle to slave device
  2501. * @command: Byte interpreted by slave
  2502. * @value: Byte being written
  2503. *
  2504. * This executes the SMBus "write byte" protocol, returning negative errno
  2505. * else zero on success.
  2506. */
  2507. s32 i2c_smbus_write_byte_data(const struct i2c_client *client, u8 command,
  2508. u8 value)
  2509. {
  2510. union i2c_smbus_data data;
  2511. data.byte = value;
  2512. return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
  2513. I2C_SMBUS_WRITE, command,
  2514. I2C_SMBUS_BYTE_DATA, &data);
  2515. }
  2516. EXPORT_SYMBOL(i2c_smbus_write_byte_data);
  2517. /**
  2518. * i2c_smbus_read_word_data - SMBus "read word" protocol
  2519. * @client: Handle to slave device
  2520. * @command: Byte interpreted by slave
  2521. *
  2522. * This executes the SMBus "read word" protocol, returning negative errno
  2523. * else a 16-bit unsigned "word" received from the device.
  2524. */
  2525. s32 i2c_smbus_read_word_data(const struct i2c_client *client, u8 command)
  2526. {
  2527. union i2c_smbus_data data;
  2528. int status;
  2529. status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
  2530. I2C_SMBUS_READ, command,
  2531. I2C_SMBUS_WORD_DATA, &data);
  2532. return (status < 0) ? status : data.word;
  2533. }
  2534. EXPORT_SYMBOL(i2c_smbus_read_word_data);
  2535. /**
  2536. * i2c_smbus_write_word_data - SMBus "write word" protocol
  2537. * @client: Handle to slave device
  2538. * @command: Byte interpreted by slave
  2539. * @value: 16-bit "word" being written
  2540. *
  2541. * This executes the SMBus "write word" protocol, returning negative errno
  2542. * else zero on success.
  2543. */
  2544. s32 i2c_smbus_write_word_data(const struct i2c_client *client, u8 command,
  2545. u16 value)
  2546. {
  2547. union i2c_smbus_data data;
  2548. data.word = value;
  2549. return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
  2550. I2C_SMBUS_WRITE, command,
  2551. I2C_SMBUS_WORD_DATA, &data);
  2552. }
  2553. EXPORT_SYMBOL(i2c_smbus_write_word_data);
  2554. /**
  2555. * i2c_smbus_read_block_data - SMBus "block read" protocol
  2556. * @client: Handle to slave device
  2557. * @command: Byte interpreted by slave
  2558. * @values: Byte array into which data will be read; big enough to hold
  2559. * the data returned by the slave. SMBus allows at most 32 bytes.
  2560. *
  2561. * This executes the SMBus "block read" protocol, returning negative errno
  2562. * else the number of data bytes in the slave's response.
  2563. *
  2564. * Note that using this function requires that the client's adapter support
  2565. * the I2C_FUNC_SMBUS_READ_BLOCK_DATA functionality. Not all adapter drivers
  2566. * support this; its emulation through I2C messaging relies on a specific
  2567. * mechanism (I2C_M_RECV_LEN) which may not be implemented.
  2568. */
  2569. s32 i2c_smbus_read_block_data(const struct i2c_client *client, u8 command,
  2570. u8 *values)
  2571. {
  2572. union i2c_smbus_data data;
  2573. int status;
  2574. status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
  2575. I2C_SMBUS_READ, command,
  2576. I2C_SMBUS_BLOCK_DATA, &data);
  2577. if (status)
  2578. return status;
  2579. memcpy(values, &data.block[1], data.block[0]);
  2580. return data.block[0];
  2581. }
  2582. EXPORT_SYMBOL(i2c_smbus_read_block_data);
  2583. /**
  2584. * i2c_smbus_write_block_data - SMBus "block write" protocol
  2585. * @client: Handle to slave device
  2586. * @command: Byte interpreted by slave
  2587. * @length: Size of data block; SMBus allows at most 32 bytes
  2588. * @values: Byte array which will be written.
  2589. *
  2590. * This executes the SMBus "block write" protocol, returning negative errno
  2591. * else zero on success.
  2592. */
  2593. s32 i2c_smbus_write_block_data(const struct i2c_client *client, u8 command,
  2594. u8 length, const u8 *values)
  2595. {
  2596. union i2c_smbus_data data;
  2597. if (length > I2C_SMBUS_BLOCK_MAX)
  2598. length = I2C_SMBUS_BLOCK_MAX;
  2599. data.block[0] = length;
  2600. memcpy(&data.block[1], values, length);
  2601. return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
  2602. I2C_SMBUS_WRITE, command,
  2603. I2C_SMBUS_BLOCK_DATA, &data);
  2604. }
  2605. EXPORT_SYMBOL(i2c_smbus_write_block_data);
  2606. /* Returns the number of read bytes */
  2607. s32 i2c_smbus_read_i2c_block_data(const struct i2c_client *client, u8 command,
  2608. u8 length, u8 *values)
  2609. {
  2610. union i2c_smbus_data data;
  2611. int status;
  2612. if (length > I2C_SMBUS_BLOCK_MAX)
  2613. length = I2C_SMBUS_BLOCK_MAX;
  2614. data.block[0] = length;
  2615. status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
  2616. I2C_SMBUS_READ, command,
  2617. I2C_SMBUS_I2C_BLOCK_DATA, &data);
  2618. if (status < 0)
  2619. return status;
  2620. memcpy(values, &data.block[1], data.block[0]);
  2621. return data.block[0];
  2622. }
  2623. EXPORT_SYMBOL(i2c_smbus_read_i2c_block_data);
  2624. s32 i2c_smbus_write_i2c_block_data(const struct i2c_client *client, u8 command,
  2625. u8 length, const u8 *values)
  2626. {
  2627. union i2c_smbus_data data;
  2628. if (length > I2C_SMBUS_BLOCK_MAX)
  2629. length = I2C_SMBUS_BLOCK_MAX;
  2630. data.block[0] = length;
  2631. memcpy(data.block + 1, values, length);
  2632. return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
  2633. I2C_SMBUS_WRITE, command,
  2634. I2C_SMBUS_I2C_BLOCK_DATA, &data);
  2635. }
  2636. EXPORT_SYMBOL(i2c_smbus_write_i2c_block_data);
  2637. /* Simulate a SMBus command using the i2c protocol
  2638. No checking of parameters is done! */
  2639. static s32 i2c_smbus_xfer_emulated(struct i2c_adapter *adapter, u16 addr,
  2640. unsigned short flags,
  2641. char read_write, u8 command, int size,
  2642. union i2c_smbus_data *data)
  2643. {
  2644. /* So we need to generate a series of msgs. In the case of writing, we
  2645. need to use only one message; when reading, we need two. We initialize
  2646. most things with sane defaults, to keep the code below somewhat
  2647. simpler. */
  2648. unsigned char msgbuf0[I2C_SMBUS_BLOCK_MAX+3];
  2649. unsigned char msgbuf1[I2C_SMBUS_BLOCK_MAX+2];
  2650. int num = read_write == I2C_SMBUS_READ ? 2 : 1;
  2651. int i;
  2652. u8 partial_pec = 0;
  2653. int status;
  2654. struct i2c_msg msg[2] = {
  2655. {
  2656. .addr = addr,
  2657. .flags = flags,
  2658. .len = 1,
  2659. .buf = msgbuf0,
  2660. }, {
  2661. .addr = addr,
  2662. .flags = flags | I2C_M_RD,
  2663. .len = 0,
  2664. .buf = msgbuf1,
  2665. },
  2666. };
  2667. msgbuf0[0] = command;
  2668. switch (size) {
  2669. case I2C_SMBUS_QUICK:
  2670. msg[0].len = 0;
  2671. /* Special case: The read/write field is used as data */
  2672. msg[0].flags = flags | (read_write == I2C_SMBUS_READ ?
  2673. I2C_M_RD : 0);
  2674. num = 1;
  2675. break;
  2676. case I2C_SMBUS_BYTE:
  2677. if (read_write == I2C_SMBUS_READ) {
  2678. /* Special case: only a read! */
  2679. msg[0].flags = I2C_M_RD | flags;
  2680. num = 1;
  2681. }
  2682. break;
  2683. case I2C_SMBUS_BYTE_DATA:
  2684. if (read_write == I2C_SMBUS_READ)
  2685. msg[1].len = 1;
  2686. else {
  2687. msg[0].len = 2;
  2688. msgbuf0[1] = data->byte;
  2689. }
  2690. break;
  2691. case I2C_SMBUS_WORD_DATA:
  2692. if (read_write == I2C_SMBUS_READ)
  2693. msg[1].len = 2;
  2694. else {
  2695. msg[0].len = 3;
  2696. msgbuf0[1] = data->word & 0xff;
  2697. msgbuf0[2] = data->word >> 8;
  2698. }
  2699. break;
  2700. case I2C_SMBUS_PROC_CALL:
  2701. num = 2; /* Special case */
  2702. read_write = I2C_SMBUS_READ;
  2703. msg[0].len = 3;
  2704. msg[1].len = 2;
  2705. msgbuf0[1] = data->word & 0xff;
  2706. msgbuf0[2] = data->word >> 8;
  2707. break;
  2708. case I2C_SMBUS_BLOCK_DATA:
  2709. if (read_write == I2C_SMBUS_READ) {
  2710. msg[1].flags |= I2C_M_RECV_LEN;
  2711. msg[1].len = 1; /* block length will be added by
  2712. the underlying bus driver */
  2713. } else {
  2714. msg[0].len = data->block[0] + 2;
  2715. if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 2) {
  2716. dev_err(&adapter->dev,
  2717. "Invalid block write size %d\n",
  2718. data->block[0]);
  2719. return -EINVAL;
  2720. }
  2721. for (i = 1; i < msg[0].len; i++)
  2722. msgbuf0[i] = data->block[i-1];
  2723. }
  2724. break;
  2725. case I2C_SMBUS_BLOCK_PROC_CALL:
  2726. num = 2; /* Another special case */
  2727. read_write = I2C_SMBUS_READ;
  2728. if (data->block[0] > I2C_SMBUS_BLOCK_MAX) {
  2729. dev_err(&adapter->dev,
  2730. "Invalid block write size %d\n",
  2731. data->block[0]);
  2732. return -EINVAL;
  2733. }
  2734. msg[0].len = data->block[0] + 2;
  2735. for (i = 1; i < msg[0].len; i++)
  2736. msgbuf0[i] = data->block[i-1];
  2737. msg[1].flags |= I2C_M_RECV_LEN;
  2738. msg[1].len = 1; /* block length will be added by
  2739. the underlying bus driver */
  2740. break;
  2741. case I2C_SMBUS_I2C_BLOCK_DATA:
  2742. if (read_write == I2C_SMBUS_READ) {
  2743. msg[1].len = data->block[0];
  2744. } else {
  2745. msg[0].len = data->block[0] + 1;
  2746. if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 1) {
  2747. dev_err(&adapter->dev,
  2748. "Invalid block write size %d\n",
  2749. data->block[0]);
  2750. return -EINVAL;
  2751. }
  2752. for (i = 1; i <= data->block[0]; i++)
  2753. msgbuf0[i] = data->block[i];
  2754. }
  2755. break;
  2756. default:
  2757. dev_err(&adapter->dev, "Unsupported transaction %d\n", size);
  2758. return -EOPNOTSUPP;
  2759. }
  2760. i = ((flags & I2C_CLIENT_PEC) && size != I2C_SMBUS_QUICK
  2761. && size != I2C_SMBUS_I2C_BLOCK_DATA);
  2762. if (i) {
  2763. /* Compute PEC if first message is a write */
  2764. if (!(msg[0].flags & I2C_M_RD)) {
  2765. if (num == 1) /* Write only */
  2766. i2c_smbus_add_pec(&msg[0]);
  2767. else /* Write followed by read */
  2768. partial_pec = i2c_smbus_msg_pec(0, &msg[0]);
  2769. }
  2770. /* Ask for PEC if last message is a read */
  2771. if (msg[num-1].flags & I2C_M_RD)
  2772. msg[num-1].len++;
  2773. }
  2774. status = i2c_transfer(adapter, msg, num);
  2775. if (status < 0)
  2776. return status;
  2777. /* Check PEC if last message is a read */
  2778. if (i && (msg[num-1].flags & I2C_M_RD)) {
  2779. status = i2c_smbus_check_pec(partial_pec, &msg[num-1]);
  2780. if (status < 0)
  2781. return status;
  2782. }
  2783. if (read_write == I2C_SMBUS_READ)
  2784. switch (size) {
  2785. case I2C_SMBUS_BYTE:
  2786. data->byte = msgbuf0[0];
  2787. break;
  2788. case I2C_SMBUS_BYTE_DATA:
  2789. data->byte = msgbuf1[0];
  2790. break;
  2791. case I2C_SMBUS_WORD_DATA:
  2792. case I2C_SMBUS_PROC_CALL:
  2793. data->word = msgbuf1[0] | (msgbuf1[1] << 8);
  2794. break;
  2795. case I2C_SMBUS_I2C_BLOCK_DATA:
  2796. for (i = 0; i < data->block[0]; i++)
  2797. data->block[i+1] = msgbuf1[i];
  2798. break;
  2799. case I2C_SMBUS_BLOCK_DATA:
  2800. case I2C_SMBUS_BLOCK_PROC_CALL:
  2801. for (i = 0; i < msgbuf1[0] + 1; i++)
  2802. data->block[i] = msgbuf1[i];
  2803. break;
  2804. }
  2805. return 0;
  2806. }
  2807. /**
  2808. * i2c_smbus_xfer - execute SMBus protocol operations
  2809. * @adapter: Handle to I2C bus
  2810. * @addr: Address of SMBus slave on that bus
  2811. * @flags: I2C_CLIENT_* flags (usually zero or I2C_CLIENT_PEC)
  2812. * @read_write: I2C_SMBUS_READ or I2C_SMBUS_WRITE
  2813. * @command: Byte interpreted by slave, for protocols which use such bytes
  2814. * @protocol: SMBus protocol operation to execute, such as I2C_SMBUS_PROC_CALL
  2815. * @data: Data to be read or written
  2816. *
  2817. * This executes an SMBus protocol operation, and returns a negative
  2818. * errno code else zero on success.
  2819. */
  2820. s32 i2c_smbus_xfer(struct i2c_adapter *adapter, u16 addr, unsigned short flags,
  2821. char read_write, u8 command, int protocol,
  2822. union i2c_smbus_data *data)
  2823. {
  2824. unsigned long orig_jiffies;
  2825. int try;
  2826. s32 res;
  2827. /* If enabled, the following two tracepoints are conditional on
  2828. * read_write and protocol.
  2829. */
  2830. trace_smbus_write(adapter, addr, flags, read_write,
  2831. command, protocol, data);
  2832. trace_smbus_read(adapter, addr, flags, read_write,
  2833. command, protocol);
  2834. flags &= I2C_M_TEN | I2C_CLIENT_PEC | I2C_CLIENT_SCCB;
  2835. if (adapter->algo->smbus_xfer) {
  2836. i2c_lock_bus(adapter, I2C_LOCK_SEGMENT);
  2837. /* Retry automatically on arbitration loss */
  2838. orig_jiffies = jiffies;
  2839. for (res = 0, try = 0; try <= adapter->retries; try++) {
  2840. res = adapter->algo->smbus_xfer(adapter, addr, flags,
  2841. read_write, command,
  2842. protocol, data);
  2843. if (res != -EAGAIN)
  2844. break;
  2845. if (time_after(jiffies,
  2846. orig_jiffies + adapter->timeout))
  2847. break;
  2848. }
  2849. i2c_unlock_bus(adapter, I2C_LOCK_SEGMENT);
  2850. if (res != -EOPNOTSUPP || !adapter->algo->master_xfer)
  2851. goto trace;
  2852. /*
  2853. * Fall back to i2c_smbus_xfer_emulated if the adapter doesn't
  2854. * implement native support for the SMBus operation.
  2855. */
  2856. }
  2857. res = i2c_smbus_xfer_emulated(adapter, addr, flags, read_write,
  2858. command, protocol, data);
  2859. trace:
  2860. /* If enabled, the reply tracepoint is conditional on read_write. */
  2861. trace_smbus_reply(adapter, addr, flags, read_write,
  2862. command, protocol, data);
  2863. trace_smbus_result(adapter, addr, flags, read_write,
  2864. command, protocol, res);
  2865. return res;
  2866. }
  2867. EXPORT_SYMBOL(i2c_smbus_xfer);
  2868. /**
  2869. * i2c_smbus_read_i2c_block_data_or_emulated - read block or emulate
  2870. * @client: Handle to slave device
  2871. * @command: Byte interpreted by slave
  2872. * @length: Size of data block; SMBus allows at most I2C_SMBUS_BLOCK_MAX bytes
  2873. * @values: Byte array into which data will be read; big enough to hold
  2874. * the data returned by the slave. SMBus allows at most
  2875. * I2C_SMBUS_BLOCK_MAX bytes.
  2876. *
  2877. * This executes the SMBus "block read" protocol if supported by the adapter.
  2878. * If block read is not supported, it emulates it using either word or byte
  2879. * read protocols depending on availability.
  2880. *
  2881. * The addresses of the I2C slave device that are accessed with this function
  2882. * must be mapped to a linear region, so that a block read will have the same
  2883. * effect as a byte read. Before using this function you must double-check
  2884. * if the I2C slave does support exchanging a block transfer with a byte
  2885. * transfer.
  2886. */
  2887. s32 i2c_smbus_read_i2c_block_data_or_emulated(const struct i2c_client *client,
  2888. u8 command, u8 length, u8 *values)
  2889. {
  2890. u8 i = 0;
  2891. int status;
  2892. if (length > I2C_SMBUS_BLOCK_MAX)
  2893. length = I2C_SMBUS_BLOCK_MAX;
  2894. if (i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_READ_I2C_BLOCK))
  2895. return i2c_smbus_read_i2c_block_data(client, command, length, values);
  2896. if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_READ_BYTE_DATA))
  2897. return -EOPNOTSUPP;
  2898. if (i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_READ_WORD_DATA)) {
  2899. while ((i + 2) <= length) {
  2900. status = i2c_smbus_read_word_data(client, command + i);
  2901. if (status < 0)
  2902. return status;
  2903. values[i] = status & 0xff;
  2904. values[i + 1] = status >> 8;
  2905. i += 2;
  2906. }
  2907. }
  2908. while (i < length) {
  2909. status = i2c_smbus_read_byte_data(client, command + i);
  2910. if (status < 0)
  2911. return status;
  2912. values[i] = status;
  2913. i++;
  2914. }
  2915. return i;
  2916. }
  2917. EXPORT_SYMBOL(i2c_smbus_read_i2c_block_data_or_emulated);
  2918. #if IS_ENABLED(CONFIG_I2C_SLAVE)
  2919. int i2c_slave_register(struct i2c_client *client, i2c_slave_cb_t slave_cb)
  2920. {
  2921. int ret;
  2922. if (!client || !slave_cb) {
  2923. WARN(1, "insufficent data\n");
  2924. return -EINVAL;
  2925. }
  2926. if (!(client->flags & I2C_CLIENT_SLAVE))
  2927. dev_warn(&client->dev, "%s: client slave flag not set. You might see address collisions\n",
  2928. __func__);
  2929. if (!(client->flags & I2C_CLIENT_TEN)) {
  2930. /* Enforce stricter address checking */
  2931. ret = i2c_check_7bit_addr_validity_strict(client->addr);
  2932. if (ret) {
  2933. dev_err(&client->dev, "%s: invalid address\n", __func__);
  2934. return ret;
  2935. }
  2936. }
  2937. if (!client->adapter->algo->reg_slave) {
  2938. dev_err(&client->dev, "%s: not supported by adapter\n", __func__);
  2939. return -EOPNOTSUPP;
  2940. }
  2941. client->slave_cb = slave_cb;
  2942. i2c_lock_adapter(client->adapter);
  2943. ret = client->adapter->algo->reg_slave(client);
  2944. i2c_unlock_adapter(client->adapter);
  2945. if (ret) {
  2946. client->slave_cb = NULL;
  2947. dev_err(&client->dev, "%s: adapter returned error %d\n", __func__, ret);
  2948. }
  2949. return ret;
  2950. }
  2951. EXPORT_SYMBOL_GPL(i2c_slave_register);
  2952. int i2c_slave_unregister(struct i2c_client *client)
  2953. {
  2954. int ret;
  2955. if (!client->adapter->algo->unreg_slave) {
  2956. dev_err(&client->dev, "%s: not supported by adapter\n", __func__);
  2957. return -EOPNOTSUPP;
  2958. }
  2959. i2c_lock_adapter(client->adapter);
  2960. ret = client->adapter->algo->unreg_slave(client);
  2961. i2c_unlock_adapter(client->adapter);
  2962. if (ret == 0)
  2963. client->slave_cb = NULL;
  2964. else
  2965. dev_err(&client->dev, "%s: adapter returned error %d\n", __func__, ret);
  2966. return ret;
  2967. }
  2968. EXPORT_SYMBOL_GPL(i2c_slave_unregister);
  2969. #endif
  2970. MODULE_AUTHOR("Simon G. Vogl <simon@tk.uni-linz.ac.at>");
  2971. MODULE_DESCRIPTION("I2C-Bus main module");
  2972. MODULE_LICENSE("GPL");