i2c-core.c 98 KB

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