i2c-core.c 87 KB

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