i2c-core-base.c 82 KB

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