i2c-core.c 99 KB

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