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