i2c-core.c 69 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558
  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. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  15. MA 02110-1301 USA. */
  16. /* ------------------------------------------------------------------------- */
  17. /* With some changes from Kyösti Mälkki <kmalkki@cc.hut.fi>.
  18. All SMBus-related things are written by Frodo Looijaard <frodol@dds.nl>
  19. SMBus 2.0 support by Mark Studebaker <mdsxyz123@yahoo.com> and
  20. Jean Delvare <jdelvare@suse.de>
  21. Mux support by Rodolfo Giometti <giometti@enneenne.com> and
  22. Michael Lawnick <michael.lawnick.ext@nsn.com>
  23. OF support is copyright (c) 2008 Jochen Friedrich <jochen@scram.de>
  24. (based on a previous patch from Jon Smirl <jonsmirl@gmail.com>) and
  25. (c) 2013 Wolfram Sang <wsa@the-dreams.de>
  26. */
  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/completion.h>
  41. #include <linux/hardirq.h>
  42. #include <linux/irqflags.h>
  43. #include <linux/rwsem.h>
  44. #include <linux/pm_runtime.h>
  45. #include <linux/acpi.h>
  46. #include <asm/uaccess.h>
  47. #include "i2c-core.h"
  48. /* core_lock protects i2c_adapter_idr, and guarantees
  49. that device detection, deletion of detected devices, and attach_adapter
  50. calls are serialized */
  51. static DEFINE_MUTEX(core_lock);
  52. static DEFINE_IDR(i2c_adapter_idr);
  53. static struct device_type i2c_client_type;
  54. static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver);
  55. /* ------------------------------------------------------------------------- */
  56. static const struct i2c_device_id *i2c_match_id(const struct i2c_device_id *id,
  57. const struct i2c_client *client)
  58. {
  59. while (id->name[0]) {
  60. if (strcmp(client->name, id->name) == 0)
  61. return id;
  62. id++;
  63. }
  64. return NULL;
  65. }
  66. static int i2c_device_match(struct device *dev, struct device_driver *drv)
  67. {
  68. struct i2c_client *client = i2c_verify_client(dev);
  69. struct i2c_driver *driver;
  70. if (!client)
  71. return 0;
  72. /* Attempt an OF style match */
  73. if (of_driver_match_device(dev, drv))
  74. return 1;
  75. /* Then ACPI style match */
  76. if (acpi_driver_match_device(dev, drv))
  77. return 1;
  78. driver = to_i2c_driver(drv);
  79. /* match on an id table if there is one */
  80. if (driver->id_table)
  81. return i2c_match_id(driver->id_table, client) != NULL;
  82. return 0;
  83. }
  84. /* uevent helps with hotplug: modprobe -q $(MODALIAS) */
  85. static int i2c_device_uevent(struct device *dev, struct kobj_uevent_env *env)
  86. {
  87. struct i2c_client *client = to_i2c_client(dev);
  88. int rc;
  89. rc = acpi_device_uevent_modalias(dev, env);
  90. if (rc != -ENODEV)
  91. return rc;
  92. if (add_uevent_var(env, "MODALIAS=%s%s",
  93. I2C_MODULE_PREFIX, client->name))
  94. return -ENOMEM;
  95. dev_dbg(dev, "uevent\n");
  96. return 0;
  97. }
  98. /* i2c bus recovery routines */
  99. static int get_scl_gpio_value(struct i2c_adapter *adap)
  100. {
  101. return gpio_get_value(adap->bus_recovery_info->scl_gpio);
  102. }
  103. static void set_scl_gpio_value(struct i2c_adapter *adap, int val)
  104. {
  105. gpio_set_value(adap->bus_recovery_info->scl_gpio, val);
  106. }
  107. static int get_sda_gpio_value(struct i2c_adapter *adap)
  108. {
  109. return gpio_get_value(adap->bus_recovery_info->sda_gpio);
  110. }
  111. static int i2c_get_gpios_for_recovery(struct i2c_adapter *adap)
  112. {
  113. struct i2c_bus_recovery_info *bri = adap->bus_recovery_info;
  114. struct device *dev = &adap->dev;
  115. int ret = 0;
  116. ret = gpio_request_one(bri->scl_gpio, GPIOF_OPEN_DRAIN |
  117. GPIOF_OUT_INIT_HIGH, "i2c-scl");
  118. if (ret) {
  119. dev_warn(dev, "Can't get SCL gpio: %d\n", bri->scl_gpio);
  120. return ret;
  121. }
  122. if (bri->get_sda) {
  123. if (gpio_request_one(bri->sda_gpio, GPIOF_IN, "i2c-sda")) {
  124. /* work without SDA polling */
  125. dev_warn(dev, "Can't get SDA gpio: %d. Not using SDA polling\n",
  126. bri->sda_gpio);
  127. bri->get_sda = NULL;
  128. }
  129. }
  130. return ret;
  131. }
  132. static void i2c_put_gpios_for_recovery(struct i2c_adapter *adap)
  133. {
  134. struct i2c_bus_recovery_info *bri = adap->bus_recovery_info;
  135. if (bri->get_sda)
  136. gpio_free(bri->sda_gpio);
  137. gpio_free(bri->scl_gpio);
  138. }
  139. /*
  140. * We are generating clock pulses. ndelay() determines durating of clk pulses.
  141. * We will generate clock with rate 100 KHz and so duration of both clock levels
  142. * is: delay in ns = (10^6 / 100) / 2
  143. */
  144. #define RECOVERY_NDELAY 5000
  145. #define RECOVERY_CLK_CNT 9
  146. static int i2c_generic_recovery(struct i2c_adapter *adap)
  147. {
  148. struct i2c_bus_recovery_info *bri = adap->bus_recovery_info;
  149. int i = 0, val = 1, ret = 0;
  150. if (bri->prepare_recovery)
  151. bri->prepare_recovery(bri);
  152. /*
  153. * By this time SCL is high, as we need to give 9 falling-rising edges
  154. */
  155. while (i++ < RECOVERY_CLK_CNT * 2) {
  156. if (val) {
  157. /* Break if SDA is high */
  158. if (bri->get_sda && bri->get_sda(adap))
  159. break;
  160. /* SCL shouldn't be low here */
  161. if (!bri->get_scl(adap)) {
  162. dev_err(&adap->dev,
  163. "SCL is stuck low, exit recovery\n");
  164. ret = -EBUSY;
  165. break;
  166. }
  167. }
  168. val = !val;
  169. bri->set_scl(adap, val);
  170. ndelay(RECOVERY_NDELAY);
  171. }
  172. if (bri->unprepare_recovery)
  173. bri->unprepare_recovery(bri);
  174. return ret;
  175. }
  176. int i2c_generic_scl_recovery(struct i2c_adapter *adap)
  177. {
  178. adap->bus_recovery_info->set_scl(adap, 1);
  179. return i2c_generic_recovery(adap);
  180. }
  181. int i2c_generic_gpio_recovery(struct i2c_adapter *adap)
  182. {
  183. int ret;
  184. ret = i2c_get_gpios_for_recovery(adap);
  185. if (ret)
  186. return ret;
  187. ret = i2c_generic_recovery(adap);
  188. i2c_put_gpios_for_recovery(adap);
  189. return ret;
  190. }
  191. int i2c_recover_bus(struct i2c_adapter *adap)
  192. {
  193. if (!adap->bus_recovery_info)
  194. return -EOPNOTSUPP;
  195. dev_dbg(&adap->dev, "Trying i2c bus recovery\n");
  196. return adap->bus_recovery_info->recover_bus(adap);
  197. }
  198. static int i2c_device_probe(struct device *dev)
  199. {
  200. struct i2c_client *client = i2c_verify_client(dev);
  201. struct i2c_driver *driver;
  202. int status;
  203. if (!client)
  204. return 0;
  205. driver = to_i2c_driver(dev->driver);
  206. if (!driver->probe || !driver->id_table)
  207. return -ENODEV;
  208. if (!device_can_wakeup(&client->dev))
  209. device_init_wakeup(&client->dev,
  210. client->flags & I2C_CLIENT_WAKE);
  211. dev_dbg(dev, "probe\n");
  212. acpi_dev_pm_attach(&client->dev, true);
  213. status = driver->probe(client, i2c_match_id(driver->id_table, client));
  214. if (status)
  215. acpi_dev_pm_detach(&client->dev, true);
  216. return status;
  217. }
  218. static int i2c_device_remove(struct device *dev)
  219. {
  220. struct i2c_client *client = i2c_verify_client(dev);
  221. struct i2c_driver *driver;
  222. int status = 0;
  223. if (!client || !dev->driver)
  224. return 0;
  225. driver = to_i2c_driver(dev->driver);
  226. if (driver->remove) {
  227. dev_dbg(dev, "remove\n");
  228. status = driver->remove(client);
  229. }
  230. acpi_dev_pm_detach(&client->dev, true);
  231. return status;
  232. }
  233. static void i2c_device_shutdown(struct device *dev)
  234. {
  235. struct i2c_client *client = i2c_verify_client(dev);
  236. struct i2c_driver *driver;
  237. if (!client || !dev->driver)
  238. return;
  239. driver = to_i2c_driver(dev->driver);
  240. if (driver->shutdown)
  241. driver->shutdown(client);
  242. }
  243. #ifdef CONFIG_PM_SLEEP
  244. static int i2c_legacy_suspend(struct device *dev, pm_message_t mesg)
  245. {
  246. struct i2c_client *client = i2c_verify_client(dev);
  247. struct i2c_driver *driver;
  248. if (!client || !dev->driver)
  249. return 0;
  250. driver = to_i2c_driver(dev->driver);
  251. if (!driver->suspend)
  252. return 0;
  253. return driver->suspend(client, mesg);
  254. }
  255. static int i2c_legacy_resume(struct device *dev)
  256. {
  257. struct i2c_client *client = i2c_verify_client(dev);
  258. struct i2c_driver *driver;
  259. if (!client || !dev->driver)
  260. return 0;
  261. driver = to_i2c_driver(dev->driver);
  262. if (!driver->resume)
  263. return 0;
  264. return driver->resume(client);
  265. }
  266. static int i2c_device_pm_suspend(struct device *dev)
  267. {
  268. const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
  269. if (pm)
  270. return pm_generic_suspend(dev);
  271. else
  272. return i2c_legacy_suspend(dev, PMSG_SUSPEND);
  273. }
  274. static int i2c_device_pm_resume(struct device *dev)
  275. {
  276. const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
  277. if (pm)
  278. return pm_generic_resume(dev);
  279. else
  280. return i2c_legacy_resume(dev);
  281. }
  282. static int i2c_device_pm_freeze(struct device *dev)
  283. {
  284. const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
  285. if (pm)
  286. return pm_generic_freeze(dev);
  287. else
  288. return i2c_legacy_suspend(dev, PMSG_FREEZE);
  289. }
  290. static int i2c_device_pm_thaw(struct device *dev)
  291. {
  292. const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
  293. if (pm)
  294. return pm_generic_thaw(dev);
  295. else
  296. return i2c_legacy_resume(dev);
  297. }
  298. static int i2c_device_pm_poweroff(struct device *dev)
  299. {
  300. const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
  301. if (pm)
  302. return pm_generic_poweroff(dev);
  303. else
  304. return i2c_legacy_suspend(dev, PMSG_HIBERNATE);
  305. }
  306. static int i2c_device_pm_restore(struct device *dev)
  307. {
  308. const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
  309. if (pm)
  310. return pm_generic_restore(dev);
  311. else
  312. return i2c_legacy_resume(dev);
  313. }
  314. #else /* !CONFIG_PM_SLEEP */
  315. #define i2c_device_pm_suspend NULL
  316. #define i2c_device_pm_resume NULL
  317. #define i2c_device_pm_freeze NULL
  318. #define i2c_device_pm_thaw NULL
  319. #define i2c_device_pm_poweroff NULL
  320. #define i2c_device_pm_restore NULL
  321. #endif /* !CONFIG_PM_SLEEP */
  322. static void i2c_client_dev_release(struct device *dev)
  323. {
  324. kfree(to_i2c_client(dev));
  325. }
  326. static ssize_t
  327. show_name(struct device *dev, struct device_attribute *attr, char *buf)
  328. {
  329. return sprintf(buf, "%s\n", dev->type == &i2c_client_type ?
  330. to_i2c_client(dev)->name : to_i2c_adapter(dev)->name);
  331. }
  332. static ssize_t
  333. show_modalias(struct device *dev, struct device_attribute *attr, char *buf)
  334. {
  335. struct i2c_client *client = to_i2c_client(dev);
  336. int len;
  337. len = acpi_device_modalias(dev, buf, PAGE_SIZE -1);
  338. if (len != -ENODEV)
  339. return len;
  340. return sprintf(buf, "%s%s\n", I2C_MODULE_PREFIX, client->name);
  341. }
  342. static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
  343. static DEVICE_ATTR(modalias, S_IRUGO, show_modalias, NULL);
  344. static struct attribute *i2c_dev_attrs[] = {
  345. &dev_attr_name.attr,
  346. /* modalias helps coldplug: modprobe $(cat .../modalias) */
  347. &dev_attr_modalias.attr,
  348. NULL
  349. };
  350. static struct attribute_group i2c_dev_attr_group = {
  351. .attrs = i2c_dev_attrs,
  352. };
  353. static const struct attribute_group *i2c_dev_attr_groups[] = {
  354. &i2c_dev_attr_group,
  355. NULL
  356. };
  357. static const struct dev_pm_ops i2c_device_pm_ops = {
  358. .suspend = i2c_device_pm_suspend,
  359. .resume = i2c_device_pm_resume,
  360. .freeze = i2c_device_pm_freeze,
  361. .thaw = i2c_device_pm_thaw,
  362. .poweroff = i2c_device_pm_poweroff,
  363. .restore = i2c_device_pm_restore,
  364. SET_RUNTIME_PM_OPS(
  365. pm_generic_runtime_suspend,
  366. pm_generic_runtime_resume,
  367. NULL
  368. )
  369. };
  370. struct bus_type i2c_bus_type = {
  371. .name = "i2c",
  372. .match = i2c_device_match,
  373. .probe = i2c_device_probe,
  374. .remove = i2c_device_remove,
  375. .shutdown = i2c_device_shutdown,
  376. .pm = &i2c_device_pm_ops,
  377. };
  378. EXPORT_SYMBOL_GPL(i2c_bus_type);
  379. static struct device_type i2c_client_type = {
  380. .groups = i2c_dev_attr_groups,
  381. .uevent = i2c_device_uevent,
  382. .release = i2c_client_dev_release,
  383. };
  384. /**
  385. * i2c_verify_client - return parameter as i2c_client, or NULL
  386. * @dev: device, probably from some driver model iterator
  387. *
  388. * When traversing the driver model tree, perhaps using driver model
  389. * iterators like @device_for_each_child(), you can't assume very much
  390. * about the nodes you find. Use this function to avoid oopses caused
  391. * by wrongly treating some non-I2C device as an i2c_client.
  392. */
  393. struct i2c_client *i2c_verify_client(struct device *dev)
  394. {
  395. return (dev->type == &i2c_client_type)
  396. ? to_i2c_client(dev)
  397. : NULL;
  398. }
  399. EXPORT_SYMBOL(i2c_verify_client);
  400. /* This is a permissive address validity check, I2C address map constraints
  401. * are purposely not enforced, except for the general call address. */
  402. static int i2c_check_client_addr_validity(const struct i2c_client *client)
  403. {
  404. if (client->flags & I2C_CLIENT_TEN) {
  405. /* 10-bit address, all values are valid */
  406. if (client->addr > 0x3ff)
  407. return -EINVAL;
  408. } else {
  409. /* 7-bit address, reject the general call address */
  410. if (client->addr == 0x00 || client->addr > 0x7f)
  411. return -EINVAL;
  412. }
  413. return 0;
  414. }
  415. /* And this is a strict address validity check, used when probing. If a
  416. * device uses a reserved address, then it shouldn't be probed. 7-bit
  417. * addressing is assumed, 10-bit address devices are rare and should be
  418. * explicitly enumerated. */
  419. static int i2c_check_addr_validity(unsigned short addr)
  420. {
  421. /*
  422. * Reserved addresses per I2C specification:
  423. * 0x00 General call address / START byte
  424. * 0x01 CBUS address
  425. * 0x02 Reserved for different bus format
  426. * 0x03 Reserved for future purposes
  427. * 0x04-0x07 Hs-mode master code
  428. * 0x78-0x7b 10-bit slave addressing
  429. * 0x7c-0x7f Reserved for future purposes
  430. */
  431. if (addr < 0x08 || addr > 0x77)
  432. return -EINVAL;
  433. return 0;
  434. }
  435. static int __i2c_check_addr_busy(struct device *dev, void *addrp)
  436. {
  437. struct i2c_client *client = i2c_verify_client(dev);
  438. int addr = *(int *)addrp;
  439. if (client && client->addr == addr)
  440. return -EBUSY;
  441. return 0;
  442. }
  443. /* walk up mux tree */
  444. static int i2c_check_mux_parents(struct i2c_adapter *adapter, int addr)
  445. {
  446. struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
  447. int result;
  448. result = device_for_each_child(&adapter->dev, &addr,
  449. __i2c_check_addr_busy);
  450. if (!result && parent)
  451. result = i2c_check_mux_parents(parent, addr);
  452. return result;
  453. }
  454. /* recurse down mux tree */
  455. static int i2c_check_mux_children(struct device *dev, void *addrp)
  456. {
  457. int result;
  458. if (dev->type == &i2c_adapter_type)
  459. result = device_for_each_child(dev, addrp,
  460. i2c_check_mux_children);
  461. else
  462. result = __i2c_check_addr_busy(dev, addrp);
  463. return result;
  464. }
  465. static int i2c_check_addr_busy(struct i2c_adapter *adapter, int addr)
  466. {
  467. struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
  468. int result = 0;
  469. if (parent)
  470. result = i2c_check_mux_parents(parent, addr);
  471. if (!result)
  472. result = device_for_each_child(&adapter->dev, &addr,
  473. i2c_check_mux_children);
  474. return result;
  475. }
  476. /**
  477. * i2c_lock_adapter - Get exclusive access to an I2C bus segment
  478. * @adapter: Target I2C bus segment
  479. */
  480. void i2c_lock_adapter(struct i2c_adapter *adapter)
  481. {
  482. struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
  483. if (parent)
  484. i2c_lock_adapter(parent);
  485. else
  486. rt_mutex_lock(&adapter->bus_lock);
  487. }
  488. EXPORT_SYMBOL_GPL(i2c_lock_adapter);
  489. /**
  490. * i2c_trylock_adapter - Try to get exclusive access to an I2C bus segment
  491. * @adapter: Target I2C bus segment
  492. */
  493. static int i2c_trylock_adapter(struct i2c_adapter *adapter)
  494. {
  495. struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
  496. if (parent)
  497. return i2c_trylock_adapter(parent);
  498. else
  499. return rt_mutex_trylock(&adapter->bus_lock);
  500. }
  501. /**
  502. * i2c_unlock_adapter - Release exclusive access to an I2C bus segment
  503. * @adapter: Target I2C bus segment
  504. */
  505. void i2c_unlock_adapter(struct i2c_adapter *adapter)
  506. {
  507. struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
  508. if (parent)
  509. i2c_unlock_adapter(parent);
  510. else
  511. rt_mutex_unlock(&adapter->bus_lock);
  512. }
  513. EXPORT_SYMBOL_GPL(i2c_unlock_adapter);
  514. static void i2c_dev_set_name(struct i2c_adapter *adap,
  515. struct i2c_client *client)
  516. {
  517. struct acpi_device *adev = ACPI_COMPANION(&client->dev);
  518. if (adev) {
  519. dev_set_name(&client->dev, "i2c-%s", acpi_dev_name(adev));
  520. return;
  521. }
  522. /* For 10-bit clients, add an arbitrary offset to avoid collisions */
  523. dev_set_name(&client->dev, "%d-%04x", i2c_adapter_id(adap),
  524. client->addr | ((client->flags & I2C_CLIENT_TEN)
  525. ? 0xa000 : 0));
  526. }
  527. /**
  528. * i2c_new_device - instantiate an i2c device
  529. * @adap: the adapter managing the device
  530. * @info: describes one I2C device; bus_num is ignored
  531. * Context: can sleep
  532. *
  533. * Create an i2c device. Binding is handled through driver model
  534. * probe()/remove() methods. A driver may be bound to this device when we
  535. * return from this function, or any later moment (e.g. maybe hotplugging will
  536. * load the driver module). This call is not appropriate for use by mainboard
  537. * initialization logic, which usually runs during an arch_initcall() long
  538. * before any i2c_adapter could exist.
  539. *
  540. * This returns the new i2c client, which may be saved for later use with
  541. * i2c_unregister_device(); or NULL to indicate an error.
  542. */
  543. struct i2c_client *
  544. i2c_new_device(struct i2c_adapter *adap, struct i2c_board_info const *info)
  545. {
  546. struct i2c_client *client;
  547. int status;
  548. client = kzalloc(sizeof *client, GFP_KERNEL);
  549. if (!client)
  550. return NULL;
  551. client->adapter = adap;
  552. client->dev.platform_data = info->platform_data;
  553. if (info->archdata)
  554. client->dev.archdata = *info->archdata;
  555. client->flags = info->flags;
  556. client->addr = info->addr;
  557. client->irq = info->irq;
  558. strlcpy(client->name, info->type, sizeof(client->name));
  559. /* Check for address validity */
  560. status = i2c_check_client_addr_validity(client);
  561. if (status) {
  562. dev_err(&adap->dev, "Invalid %d-bit I2C address 0x%02hx\n",
  563. client->flags & I2C_CLIENT_TEN ? 10 : 7, client->addr);
  564. goto out_err_silent;
  565. }
  566. /* Check for address business */
  567. status = i2c_check_addr_busy(adap, client->addr);
  568. if (status)
  569. goto out_err;
  570. client->dev.parent = &client->adapter->dev;
  571. client->dev.bus = &i2c_bus_type;
  572. client->dev.type = &i2c_client_type;
  573. client->dev.of_node = info->of_node;
  574. ACPI_COMPANION_SET(&client->dev, info->acpi_node.companion);
  575. i2c_dev_set_name(adap, client);
  576. status = device_register(&client->dev);
  577. if (status)
  578. goto out_err;
  579. dev_dbg(&adap->dev, "client [%s] registered with bus id %s\n",
  580. client->name, dev_name(&client->dev));
  581. return client;
  582. out_err:
  583. dev_err(&adap->dev, "Failed to register i2c client %s at 0x%02x "
  584. "(%d)\n", client->name, client->addr, status);
  585. out_err_silent:
  586. kfree(client);
  587. return NULL;
  588. }
  589. EXPORT_SYMBOL_GPL(i2c_new_device);
  590. /**
  591. * i2c_unregister_device - reverse effect of i2c_new_device()
  592. * @client: value returned from i2c_new_device()
  593. * Context: can sleep
  594. */
  595. void i2c_unregister_device(struct i2c_client *client)
  596. {
  597. device_unregister(&client->dev);
  598. }
  599. EXPORT_SYMBOL_GPL(i2c_unregister_device);
  600. static const struct i2c_device_id dummy_id[] = {
  601. { "dummy", 0 },
  602. { },
  603. };
  604. static int dummy_probe(struct i2c_client *client,
  605. const struct i2c_device_id *id)
  606. {
  607. return 0;
  608. }
  609. static int dummy_remove(struct i2c_client *client)
  610. {
  611. return 0;
  612. }
  613. static struct i2c_driver dummy_driver = {
  614. .driver.name = "dummy",
  615. .probe = dummy_probe,
  616. .remove = dummy_remove,
  617. .id_table = dummy_id,
  618. };
  619. /**
  620. * i2c_new_dummy - return a new i2c device bound to a dummy driver
  621. * @adapter: the adapter managing the device
  622. * @address: seven bit address to be used
  623. * Context: can sleep
  624. *
  625. * This returns an I2C client bound to the "dummy" driver, intended for use
  626. * with devices that consume multiple addresses. Examples of such chips
  627. * include various EEPROMS (like 24c04 and 24c08 models).
  628. *
  629. * These dummy devices have two main uses. First, most I2C and SMBus calls
  630. * except i2c_transfer() need a client handle; the dummy will be that handle.
  631. * And second, this prevents the specified address from being bound to a
  632. * different driver.
  633. *
  634. * This returns the new i2c client, which should be saved for later use with
  635. * i2c_unregister_device(); or NULL to indicate an error.
  636. */
  637. struct i2c_client *i2c_new_dummy(struct i2c_adapter *adapter, u16 address)
  638. {
  639. struct i2c_board_info info = {
  640. I2C_BOARD_INFO("dummy", address),
  641. };
  642. return i2c_new_device(adapter, &info);
  643. }
  644. EXPORT_SYMBOL_GPL(i2c_new_dummy);
  645. /* ------------------------------------------------------------------------- */
  646. /* I2C bus adapters -- one roots each I2C or SMBUS segment */
  647. static void i2c_adapter_dev_release(struct device *dev)
  648. {
  649. struct i2c_adapter *adap = to_i2c_adapter(dev);
  650. complete(&adap->dev_released);
  651. }
  652. /*
  653. * This function is only needed for mutex_lock_nested, so it is never
  654. * called unless locking correctness checking is enabled. Thus we
  655. * make it inline to avoid a compiler warning. That's what gcc ends up
  656. * doing anyway.
  657. */
  658. static inline unsigned int i2c_adapter_depth(struct i2c_adapter *adapter)
  659. {
  660. unsigned int depth = 0;
  661. while ((adapter = i2c_parent_is_i2c_adapter(adapter)))
  662. depth++;
  663. return depth;
  664. }
  665. /*
  666. * Let users instantiate I2C devices through sysfs. This can be used when
  667. * platform initialization code doesn't contain the proper data for
  668. * whatever reason. Also useful for drivers that do device detection and
  669. * detection fails, either because the device uses an unexpected address,
  670. * or this is a compatible device with different ID register values.
  671. *
  672. * Parameter checking may look overzealous, but we really don't want
  673. * the user to provide incorrect parameters.
  674. */
  675. static ssize_t
  676. i2c_sysfs_new_device(struct device *dev, struct device_attribute *attr,
  677. const char *buf, size_t count)
  678. {
  679. struct i2c_adapter *adap = to_i2c_adapter(dev);
  680. struct i2c_board_info info;
  681. struct i2c_client *client;
  682. char *blank, end;
  683. int res;
  684. memset(&info, 0, sizeof(struct i2c_board_info));
  685. blank = strchr(buf, ' ');
  686. if (!blank) {
  687. dev_err(dev, "%s: Missing parameters\n", "new_device");
  688. return -EINVAL;
  689. }
  690. if (blank - buf > I2C_NAME_SIZE - 1) {
  691. dev_err(dev, "%s: Invalid device name\n", "new_device");
  692. return -EINVAL;
  693. }
  694. memcpy(info.type, buf, blank - buf);
  695. /* Parse remaining parameters, reject extra parameters */
  696. res = sscanf(++blank, "%hi%c", &info.addr, &end);
  697. if (res < 1) {
  698. dev_err(dev, "%s: Can't parse I2C address\n", "new_device");
  699. return -EINVAL;
  700. }
  701. if (res > 1 && end != '\n') {
  702. dev_err(dev, "%s: Extra parameters\n", "new_device");
  703. return -EINVAL;
  704. }
  705. client = i2c_new_device(adap, &info);
  706. if (!client)
  707. return -EINVAL;
  708. /* Keep track of the added device */
  709. mutex_lock(&adap->userspace_clients_lock);
  710. list_add_tail(&client->detected, &adap->userspace_clients);
  711. mutex_unlock(&adap->userspace_clients_lock);
  712. dev_info(dev, "%s: Instantiated device %s at 0x%02hx\n", "new_device",
  713. info.type, info.addr);
  714. return count;
  715. }
  716. /*
  717. * And of course let the users delete the devices they instantiated, if
  718. * they got it wrong. This interface can only be used to delete devices
  719. * instantiated by i2c_sysfs_new_device above. This guarantees that we
  720. * don't delete devices to which some kernel code still has references.
  721. *
  722. * Parameter checking may look overzealous, but we really don't want
  723. * the user to delete the wrong device.
  724. */
  725. static ssize_t
  726. i2c_sysfs_delete_device(struct device *dev, struct device_attribute *attr,
  727. const char *buf, size_t count)
  728. {
  729. struct i2c_adapter *adap = to_i2c_adapter(dev);
  730. struct i2c_client *client, *next;
  731. unsigned short addr;
  732. char end;
  733. int res;
  734. /* Parse parameters, reject extra parameters */
  735. res = sscanf(buf, "%hi%c", &addr, &end);
  736. if (res < 1) {
  737. dev_err(dev, "%s: Can't parse I2C address\n", "delete_device");
  738. return -EINVAL;
  739. }
  740. if (res > 1 && end != '\n') {
  741. dev_err(dev, "%s: Extra parameters\n", "delete_device");
  742. return -EINVAL;
  743. }
  744. /* Make sure the device was added through sysfs */
  745. res = -ENOENT;
  746. mutex_lock_nested(&adap->userspace_clients_lock,
  747. i2c_adapter_depth(adap));
  748. list_for_each_entry_safe(client, next, &adap->userspace_clients,
  749. detected) {
  750. if (client->addr == addr) {
  751. dev_info(dev, "%s: Deleting device %s at 0x%02hx\n",
  752. "delete_device", client->name, client->addr);
  753. list_del(&client->detected);
  754. i2c_unregister_device(client);
  755. res = count;
  756. break;
  757. }
  758. }
  759. mutex_unlock(&adap->userspace_clients_lock);
  760. if (res < 0)
  761. dev_err(dev, "%s: Can't find device in list\n",
  762. "delete_device");
  763. return res;
  764. }
  765. static DEVICE_ATTR(new_device, S_IWUSR, NULL, i2c_sysfs_new_device);
  766. static DEVICE_ATTR_IGNORE_LOCKDEP(delete_device, S_IWUSR, NULL,
  767. i2c_sysfs_delete_device);
  768. static struct attribute *i2c_adapter_attrs[] = {
  769. &dev_attr_name.attr,
  770. &dev_attr_new_device.attr,
  771. &dev_attr_delete_device.attr,
  772. NULL
  773. };
  774. static struct attribute_group i2c_adapter_attr_group = {
  775. .attrs = i2c_adapter_attrs,
  776. };
  777. static const struct attribute_group *i2c_adapter_attr_groups[] = {
  778. &i2c_adapter_attr_group,
  779. NULL
  780. };
  781. struct device_type i2c_adapter_type = {
  782. .groups = i2c_adapter_attr_groups,
  783. .release = i2c_adapter_dev_release,
  784. };
  785. EXPORT_SYMBOL_GPL(i2c_adapter_type);
  786. /**
  787. * i2c_verify_adapter - return parameter as i2c_adapter or NULL
  788. * @dev: device, probably from some driver model iterator
  789. *
  790. * When traversing the driver model tree, perhaps using driver model
  791. * iterators like @device_for_each_child(), you can't assume very much
  792. * about the nodes you find. Use this function to avoid oopses caused
  793. * by wrongly treating some non-I2C device as an i2c_adapter.
  794. */
  795. struct i2c_adapter *i2c_verify_adapter(struct device *dev)
  796. {
  797. return (dev->type == &i2c_adapter_type)
  798. ? to_i2c_adapter(dev)
  799. : NULL;
  800. }
  801. EXPORT_SYMBOL(i2c_verify_adapter);
  802. #ifdef CONFIG_I2C_COMPAT
  803. static struct class_compat *i2c_adapter_compat_class;
  804. #endif
  805. static void i2c_scan_static_board_info(struct i2c_adapter *adapter)
  806. {
  807. struct i2c_devinfo *devinfo;
  808. down_read(&__i2c_board_lock);
  809. list_for_each_entry(devinfo, &__i2c_board_list, list) {
  810. if (devinfo->busnum == adapter->nr
  811. && !i2c_new_device(adapter,
  812. &devinfo->board_info))
  813. dev_err(&adapter->dev,
  814. "Can't create device at 0x%02x\n",
  815. devinfo->board_info.addr);
  816. }
  817. up_read(&__i2c_board_lock);
  818. }
  819. /* OF support code */
  820. #if IS_ENABLED(CONFIG_OF)
  821. static void of_i2c_register_devices(struct i2c_adapter *adap)
  822. {
  823. void *result;
  824. struct device_node *node;
  825. /* Only register child devices if the adapter has a node pointer set */
  826. if (!adap->dev.of_node)
  827. return;
  828. dev_dbg(&adap->dev, "of_i2c: walking child nodes\n");
  829. for_each_available_child_of_node(adap->dev.of_node, node) {
  830. struct i2c_board_info info = {};
  831. struct dev_archdata dev_ad = {};
  832. const __be32 *addr;
  833. int len;
  834. dev_dbg(&adap->dev, "of_i2c: register %s\n", node->full_name);
  835. if (of_modalias_node(node, info.type, sizeof(info.type)) < 0) {
  836. dev_err(&adap->dev, "of_i2c: modalias failure on %s\n",
  837. node->full_name);
  838. continue;
  839. }
  840. addr = of_get_property(node, "reg", &len);
  841. if (!addr || (len < sizeof(int))) {
  842. dev_err(&adap->dev, "of_i2c: invalid reg on %s\n",
  843. node->full_name);
  844. continue;
  845. }
  846. info.addr = be32_to_cpup(addr);
  847. if (info.addr > (1 << 10) - 1) {
  848. dev_err(&adap->dev, "of_i2c: invalid addr=%x on %s\n",
  849. info.addr, node->full_name);
  850. continue;
  851. }
  852. info.irq = irq_of_parse_and_map(node, 0);
  853. info.of_node = of_node_get(node);
  854. info.archdata = &dev_ad;
  855. if (of_get_property(node, "wakeup-source", NULL))
  856. info.flags |= I2C_CLIENT_WAKE;
  857. request_module("%s%s", I2C_MODULE_PREFIX, info.type);
  858. result = i2c_new_device(adap, &info);
  859. if (result == NULL) {
  860. dev_err(&adap->dev, "of_i2c: Failure registering %s\n",
  861. node->full_name);
  862. of_node_put(node);
  863. irq_dispose_mapping(info.irq);
  864. continue;
  865. }
  866. }
  867. }
  868. static int of_dev_node_match(struct device *dev, void *data)
  869. {
  870. return dev->of_node == data;
  871. }
  872. /* must call put_device() when done with returned i2c_client device */
  873. struct i2c_client *of_find_i2c_device_by_node(struct device_node *node)
  874. {
  875. struct device *dev;
  876. dev = bus_find_device(&i2c_bus_type, NULL, node,
  877. of_dev_node_match);
  878. if (!dev)
  879. return NULL;
  880. return i2c_verify_client(dev);
  881. }
  882. EXPORT_SYMBOL(of_find_i2c_device_by_node);
  883. /* must call put_device() when done with returned i2c_adapter device */
  884. struct i2c_adapter *of_find_i2c_adapter_by_node(struct device_node *node)
  885. {
  886. struct device *dev;
  887. dev = bus_find_device(&i2c_bus_type, NULL, node,
  888. of_dev_node_match);
  889. if (!dev)
  890. return NULL;
  891. return i2c_verify_adapter(dev);
  892. }
  893. EXPORT_SYMBOL(of_find_i2c_adapter_by_node);
  894. #else
  895. static void of_i2c_register_devices(struct i2c_adapter *adap) { }
  896. #endif /* CONFIG_OF */
  897. /* ACPI support code */
  898. #if IS_ENABLED(CONFIG_ACPI)
  899. static int acpi_i2c_add_resource(struct acpi_resource *ares, void *data)
  900. {
  901. struct i2c_board_info *info = data;
  902. if (ares->type == ACPI_RESOURCE_TYPE_SERIAL_BUS) {
  903. struct acpi_resource_i2c_serialbus *sb;
  904. sb = &ares->data.i2c_serial_bus;
  905. if (sb->type == ACPI_RESOURCE_SERIAL_TYPE_I2C) {
  906. info->addr = sb->slave_address;
  907. if (sb->access_mode == ACPI_I2C_10BIT_MODE)
  908. info->flags |= I2C_CLIENT_TEN;
  909. }
  910. } else if (info->irq < 0) {
  911. struct resource r;
  912. if (acpi_dev_resource_interrupt(ares, 0, &r))
  913. info->irq = r.start;
  914. }
  915. /* Tell the ACPI core to skip this resource */
  916. return 1;
  917. }
  918. static acpi_status acpi_i2c_add_device(acpi_handle handle, u32 level,
  919. void *data, void **return_value)
  920. {
  921. struct i2c_adapter *adapter = data;
  922. struct list_head resource_list;
  923. struct i2c_board_info info;
  924. struct acpi_device *adev;
  925. int ret;
  926. if (acpi_bus_get_device(handle, &adev))
  927. return AE_OK;
  928. if (acpi_bus_get_status(adev) || !adev->status.present)
  929. return AE_OK;
  930. memset(&info, 0, sizeof(info));
  931. info.acpi_node.companion = adev;
  932. info.irq = -1;
  933. INIT_LIST_HEAD(&resource_list);
  934. ret = acpi_dev_get_resources(adev, &resource_list,
  935. acpi_i2c_add_resource, &info);
  936. acpi_dev_free_resource_list(&resource_list);
  937. if (ret < 0 || !info.addr)
  938. return AE_OK;
  939. adev->power.flags.ignore_parent = true;
  940. strlcpy(info.type, dev_name(&adev->dev), sizeof(info.type));
  941. if (!i2c_new_device(adapter, &info)) {
  942. adev->power.flags.ignore_parent = false;
  943. dev_err(&adapter->dev,
  944. "failed to add I2C device %s from ACPI\n",
  945. dev_name(&adev->dev));
  946. }
  947. return AE_OK;
  948. }
  949. /**
  950. * acpi_i2c_register_devices - enumerate I2C slave devices behind adapter
  951. * @adap: pointer to adapter
  952. *
  953. * Enumerate all I2C slave devices behind this adapter by walking the ACPI
  954. * namespace. When a device is found it will be added to the Linux device
  955. * model and bound to the corresponding ACPI handle.
  956. */
  957. static void acpi_i2c_register_devices(struct i2c_adapter *adap)
  958. {
  959. acpi_handle handle;
  960. acpi_status status;
  961. if (!adap->dev.parent)
  962. return;
  963. handle = ACPI_HANDLE(adap->dev.parent);
  964. if (!handle)
  965. return;
  966. status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, 1,
  967. acpi_i2c_add_device, NULL,
  968. adap, NULL);
  969. if (ACPI_FAILURE(status))
  970. dev_warn(&adap->dev, "failed to enumerate I2C slaves\n");
  971. }
  972. #else
  973. static inline void acpi_i2c_register_devices(struct i2c_adapter *adap) {}
  974. #endif /* CONFIG_ACPI */
  975. static int i2c_do_add_adapter(struct i2c_driver *driver,
  976. struct i2c_adapter *adap)
  977. {
  978. /* Detect supported devices on that bus, and instantiate them */
  979. i2c_detect(adap, driver);
  980. /* Let legacy drivers scan this bus for matching devices */
  981. if (driver->attach_adapter) {
  982. dev_warn(&adap->dev, "%s: attach_adapter method is deprecated\n",
  983. driver->driver.name);
  984. dev_warn(&adap->dev, "Please use another way to instantiate "
  985. "your i2c_client\n");
  986. /* We ignore the return code; if it fails, too bad */
  987. driver->attach_adapter(adap);
  988. }
  989. return 0;
  990. }
  991. static int __process_new_adapter(struct device_driver *d, void *data)
  992. {
  993. return i2c_do_add_adapter(to_i2c_driver(d), data);
  994. }
  995. static int i2c_register_adapter(struct i2c_adapter *adap)
  996. {
  997. int res = 0;
  998. /* Can't register until after driver model init */
  999. if (unlikely(WARN_ON(!i2c_bus_type.p))) {
  1000. res = -EAGAIN;
  1001. goto out_list;
  1002. }
  1003. /* Sanity checks */
  1004. if (unlikely(adap->name[0] == '\0')) {
  1005. pr_err("i2c-core: Attempt to register an adapter with "
  1006. "no name!\n");
  1007. return -EINVAL;
  1008. }
  1009. if (unlikely(!adap->algo)) {
  1010. pr_err("i2c-core: Attempt to register adapter '%s' with "
  1011. "no algo!\n", adap->name);
  1012. return -EINVAL;
  1013. }
  1014. rt_mutex_init(&adap->bus_lock);
  1015. mutex_init(&adap->userspace_clients_lock);
  1016. INIT_LIST_HEAD(&adap->userspace_clients);
  1017. /* Set default timeout to 1 second if not already set */
  1018. if (adap->timeout == 0)
  1019. adap->timeout = HZ;
  1020. dev_set_name(&adap->dev, "i2c-%d", adap->nr);
  1021. adap->dev.bus = &i2c_bus_type;
  1022. adap->dev.type = &i2c_adapter_type;
  1023. res = device_register(&adap->dev);
  1024. if (res)
  1025. goto out_list;
  1026. dev_dbg(&adap->dev, "adapter [%s] registered\n", adap->name);
  1027. #ifdef CONFIG_I2C_COMPAT
  1028. res = class_compat_create_link(i2c_adapter_compat_class, &adap->dev,
  1029. adap->dev.parent);
  1030. if (res)
  1031. dev_warn(&adap->dev,
  1032. "Failed to create compatibility class link\n");
  1033. #endif
  1034. /* bus recovery specific initialization */
  1035. if (adap->bus_recovery_info) {
  1036. struct i2c_bus_recovery_info *bri = adap->bus_recovery_info;
  1037. if (!bri->recover_bus) {
  1038. dev_err(&adap->dev, "No recover_bus() found, not using recovery\n");
  1039. adap->bus_recovery_info = NULL;
  1040. goto exit_recovery;
  1041. }
  1042. /* Generic GPIO recovery */
  1043. if (bri->recover_bus == i2c_generic_gpio_recovery) {
  1044. if (!gpio_is_valid(bri->scl_gpio)) {
  1045. dev_err(&adap->dev, "Invalid SCL gpio, not using recovery\n");
  1046. adap->bus_recovery_info = NULL;
  1047. goto exit_recovery;
  1048. }
  1049. if (gpio_is_valid(bri->sda_gpio))
  1050. bri->get_sda = get_sda_gpio_value;
  1051. else
  1052. bri->get_sda = NULL;
  1053. bri->get_scl = get_scl_gpio_value;
  1054. bri->set_scl = set_scl_gpio_value;
  1055. } else if (!bri->set_scl || !bri->get_scl) {
  1056. /* Generic SCL recovery */
  1057. dev_err(&adap->dev, "No {get|set}_gpio() found, not using recovery\n");
  1058. adap->bus_recovery_info = NULL;
  1059. }
  1060. }
  1061. exit_recovery:
  1062. /* create pre-declared device nodes */
  1063. of_i2c_register_devices(adap);
  1064. acpi_i2c_register_devices(adap);
  1065. if (adap->nr < __i2c_first_dynamic_bus_num)
  1066. i2c_scan_static_board_info(adap);
  1067. /* Notify drivers */
  1068. mutex_lock(&core_lock);
  1069. bus_for_each_drv(&i2c_bus_type, NULL, adap, __process_new_adapter);
  1070. mutex_unlock(&core_lock);
  1071. return 0;
  1072. out_list:
  1073. mutex_lock(&core_lock);
  1074. idr_remove(&i2c_adapter_idr, adap->nr);
  1075. mutex_unlock(&core_lock);
  1076. return res;
  1077. }
  1078. /**
  1079. * __i2c_add_numbered_adapter - i2c_add_numbered_adapter where nr is never -1
  1080. * @adap: the adapter to register (with adap->nr initialized)
  1081. * Context: can sleep
  1082. *
  1083. * See i2c_add_numbered_adapter() for details.
  1084. */
  1085. static int __i2c_add_numbered_adapter(struct i2c_adapter *adap)
  1086. {
  1087. int id;
  1088. mutex_lock(&core_lock);
  1089. id = idr_alloc(&i2c_adapter_idr, adap, adap->nr, adap->nr + 1,
  1090. GFP_KERNEL);
  1091. mutex_unlock(&core_lock);
  1092. if (id < 0)
  1093. return id == -ENOSPC ? -EBUSY : id;
  1094. return i2c_register_adapter(adap);
  1095. }
  1096. /**
  1097. * i2c_add_adapter - declare i2c adapter, use dynamic bus number
  1098. * @adapter: the adapter to add
  1099. * Context: can sleep
  1100. *
  1101. * This routine is used to declare an I2C adapter when its bus number
  1102. * doesn't matter or when its bus number is specified by an dt alias.
  1103. * Examples of bases when the bus number doesn't matter: I2C adapters
  1104. * dynamically added by USB links or PCI plugin cards.
  1105. *
  1106. * When this returns zero, a new bus number was allocated and stored
  1107. * in adap->nr, and the specified adapter became available for clients.
  1108. * Otherwise, a negative errno value is returned.
  1109. */
  1110. int i2c_add_adapter(struct i2c_adapter *adapter)
  1111. {
  1112. struct device *dev = &adapter->dev;
  1113. int id;
  1114. if (dev->of_node) {
  1115. id = of_alias_get_id(dev->of_node, "i2c");
  1116. if (id >= 0) {
  1117. adapter->nr = id;
  1118. return __i2c_add_numbered_adapter(adapter);
  1119. }
  1120. }
  1121. mutex_lock(&core_lock);
  1122. id = idr_alloc(&i2c_adapter_idr, adapter,
  1123. __i2c_first_dynamic_bus_num, 0, GFP_KERNEL);
  1124. mutex_unlock(&core_lock);
  1125. if (id < 0)
  1126. return id;
  1127. adapter->nr = id;
  1128. return i2c_register_adapter(adapter);
  1129. }
  1130. EXPORT_SYMBOL(i2c_add_adapter);
  1131. /**
  1132. * i2c_add_numbered_adapter - declare i2c adapter, use static bus number
  1133. * @adap: the adapter to register (with adap->nr initialized)
  1134. * Context: can sleep
  1135. *
  1136. * This routine is used to declare an I2C adapter when its bus number
  1137. * matters. For example, use it for I2C adapters from system-on-chip CPUs,
  1138. * or otherwise built in to the system's mainboard, and where i2c_board_info
  1139. * is used to properly configure I2C devices.
  1140. *
  1141. * If the requested bus number is set to -1, then this function will behave
  1142. * identically to i2c_add_adapter, and will dynamically assign a bus number.
  1143. *
  1144. * If no devices have pre-been declared for this bus, then be sure to
  1145. * register the adapter before any dynamically allocated ones. Otherwise
  1146. * the required bus ID may not be available.
  1147. *
  1148. * When this returns zero, the specified adapter became available for
  1149. * clients using the bus number provided in adap->nr. Also, the table
  1150. * of I2C devices pre-declared using i2c_register_board_info() is scanned,
  1151. * and the appropriate driver model device nodes are created. Otherwise, a
  1152. * negative errno value is returned.
  1153. */
  1154. int i2c_add_numbered_adapter(struct i2c_adapter *adap)
  1155. {
  1156. if (adap->nr == -1) /* -1 means dynamically assign bus id */
  1157. return i2c_add_adapter(adap);
  1158. return __i2c_add_numbered_adapter(adap);
  1159. }
  1160. EXPORT_SYMBOL_GPL(i2c_add_numbered_adapter);
  1161. static void i2c_do_del_adapter(struct i2c_driver *driver,
  1162. struct i2c_adapter *adapter)
  1163. {
  1164. struct i2c_client *client, *_n;
  1165. /* Remove the devices we created ourselves as the result of hardware
  1166. * probing (using a driver's detect method) */
  1167. list_for_each_entry_safe(client, _n, &driver->clients, detected) {
  1168. if (client->adapter == adapter) {
  1169. dev_dbg(&adapter->dev, "Removing %s at 0x%x\n",
  1170. client->name, client->addr);
  1171. list_del(&client->detected);
  1172. i2c_unregister_device(client);
  1173. }
  1174. }
  1175. }
  1176. static int __unregister_client(struct device *dev, void *dummy)
  1177. {
  1178. struct i2c_client *client = i2c_verify_client(dev);
  1179. if (client && strcmp(client->name, "dummy"))
  1180. i2c_unregister_device(client);
  1181. return 0;
  1182. }
  1183. static int __unregister_dummy(struct device *dev, void *dummy)
  1184. {
  1185. struct i2c_client *client = i2c_verify_client(dev);
  1186. if (client)
  1187. i2c_unregister_device(client);
  1188. return 0;
  1189. }
  1190. static int __process_removed_adapter(struct device_driver *d, void *data)
  1191. {
  1192. i2c_do_del_adapter(to_i2c_driver(d), data);
  1193. return 0;
  1194. }
  1195. /**
  1196. * i2c_del_adapter - unregister I2C adapter
  1197. * @adap: the adapter being unregistered
  1198. * Context: can sleep
  1199. *
  1200. * This unregisters an I2C adapter which was previously registered
  1201. * by @i2c_add_adapter or @i2c_add_numbered_adapter.
  1202. */
  1203. void i2c_del_adapter(struct i2c_adapter *adap)
  1204. {
  1205. struct i2c_adapter *found;
  1206. struct i2c_client *client, *next;
  1207. /* First make sure that this adapter was ever added */
  1208. mutex_lock(&core_lock);
  1209. found = idr_find(&i2c_adapter_idr, adap->nr);
  1210. mutex_unlock(&core_lock);
  1211. if (found != adap) {
  1212. pr_debug("i2c-core: attempting to delete unregistered "
  1213. "adapter [%s]\n", adap->name);
  1214. return;
  1215. }
  1216. /* Tell drivers about this removal */
  1217. mutex_lock(&core_lock);
  1218. bus_for_each_drv(&i2c_bus_type, NULL, adap,
  1219. __process_removed_adapter);
  1220. mutex_unlock(&core_lock);
  1221. /* Remove devices instantiated from sysfs */
  1222. mutex_lock_nested(&adap->userspace_clients_lock,
  1223. i2c_adapter_depth(adap));
  1224. list_for_each_entry_safe(client, next, &adap->userspace_clients,
  1225. detected) {
  1226. dev_dbg(&adap->dev, "Removing %s at 0x%x\n", client->name,
  1227. client->addr);
  1228. list_del(&client->detected);
  1229. i2c_unregister_device(client);
  1230. }
  1231. mutex_unlock(&adap->userspace_clients_lock);
  1232. /* Detach any active clients. This can't fail, thus we do not
  1233. * check the returned value. This is a two-pass process, because
  1234. * we can't remove the dummy devices during the first pass: they
  1235. * could have been instantiated by real devices wishing to clean
  1236. * them up properly, so we give them a chance to do that first. */
  1237. device_for_each_child(&adap->dev, NULL, __unregister_client);
  1238. device_for_each_child(&adap->dev, NULL, __unregister_dummy);
  1239. #ifdef CONFIG_I2C_COMPAT
  1240. class_compat_remove_link(i2c_adapter_compat_class, &adap->dev,
  1241. adap->dev.parent);
  1242. #endif
  1243. /* device name is gone after device_unregister */
  1244. dev_dbg(&adap->dev, "adapter [%s] unregistered\n", adap->name);
  1245. /* clean up the sysfs representation */
  1246. init_completion(&adap->dev_released);
  1247. device_unregister(&adap->dev);
  1248. /* wait for sysfs to drop all references */
  1249. wait_for_completion(&adap->dev_released);
  1250. /* free bus id */
  1251. mutex_lock(&core_lock);
  1252. idr_remove(&i2c_adapter_idr, adap->nr);
  1253. mutex_unlock(&core_lock);
  1254. /* Clear the device structure in case this adapter is ever going to be
  1255. added again */
  1256. memset(&adap->dev, 0, sizeof(adap->dev));
  1257. }
  1258. EXPORT_SYMBOL(i2c_del_adapter);
  1259. /* ------------------------------------------------------------------------- */
  1260. int i2c_for_each_dev(void *data, int (*fn)(struct device *, void *))
  1261. {
  1262. int res;
  1263. mutex_lock(&core_lock);
  1264. res = bus_for_each_dev(&i2c_bus_type, NULL, data, fn);
  1265. mutex_unlock(&core_lock);
  1266. return res;
  1267. }
  1268. EXPORT_SYMBOL_GPL(i2c_for_each_dev);
  1269. static int __process_new_driver(struct device *dev, void *data)
  1270. {
  1271. if (dev->type != &i2c_adapter_type)
  1272. return 0;
  1273. return i2c_do_add_adapter(data, to_i2c_adapter(dev));
  1274. }
  1275. /*
  1276. * An i2c_driver is used with one or more i2c_client (device) nodes to access
  1277. * i2c slave chips, on a bus instance associated with some i2c_adapter.
  1278. */
  1279. int i2c_register_driver(struct module *owner, struct i2c_driver *driver)
  1280. {
  1281. int res;
  1282. /* Can't register until after driver model init */
  1283. if (unlikely(WARN_ON(!i2c_bus_type.p)))
  1284. return -EAGAIN;
  1285. /* add the driver to the list of i2c drivers in the driver core */
  1286. driver->driver.owner = owner;
  1287. driver->driver.bus = &i2c_bus_type;
  1288. /* When registration returns, the driver core
  1289. * will have called probe() for all matching-but-unbound devices.
  1290. */
  1291. res = driver_register(&driver->driver);
  1292. if (res)
  1293. return res;
  1294. /* Drivers should switch to dev_pm_ops instead. */
  1295. if (driver->suspend)
  1296. pr_warn("i2c-core: driver [%s] using legacy suspend method\n",
  1297. driver->driver.name);
  1298. if (driver->resume)
  1299. pr_warn("i2c-core: driver [%s] using legacy resume method\n",
  1300. driver->driver.name);
  1301. pr_debug("i2c-core: driver [%s] registered\n", driver->driver.name);
  1302. INIT_LIST_HEAD(&driver->clients);
  1303. /* Walk the adapters that are already present */
  1304. i2c_for_each_dev(driver, __process_new_driver);
  1305. return 0;
  1306. }
  1307. EXPORT_SYMBOL(i2c_register_driver);
  1308. static int __process_removed_driver(struct device *dev, void *data)
  1309. {
  1310. if (dev->type == &i2c_adapter_type)
  1311. i2c_do_del_adapter(data, to_i2c_adapter(dev));
  1312. return 0;
  1313. }
  1314. /**
  1315. * i2c_del_driver - unregister I2C driver
  1316. * @driver: the driver being unregistered
  1317. * Context: can sleep
  1318. */
  1319. void i2c_del_driver(struct i2c_driver *driver)
  1320. {
  1321. i2c_for_each_dev(driver, __process_removed_driver);
  1322. driver_unregister(&driver->driver);
  1323. pr_debug("i2c-core: driver [%s] unregistered\n", driver->driver.name);
  1324. }
  1325. EXPORT_SYMBOL(i2c_del_driver);
  1326. /* ------------------------------------------------------------------------- */
  1327. /**
  1328. * i2c_use_client - increments the reference count of the i2c client structure
  1329. * @client: the client being referenced
  1330. *
  1331. * Each live reference to a client should be refcounted. The driver model does
  1332. * that automatically as part of driver binding, so that most drivers don't
  1333. * need to do this explicitly: they hold a reference until they're unbound
  1334. * from the device.
  1335. *
  1336. * A pointer to the client with the incremented reference counter is returned.
  1337. */
  1338. struct i2c_client *i2c_use_client(struct i2c_client *client)
  1339. {
  1340. if (client && get_device(&client->dev))
  1341. return client;
  1342. return NULL;
  1343. }
  1344. EXPORT_SYMBOL(i2c_use_client);
  1345. /**
  1346. * i2c_release_client - release a use of the i2c client structure
  1347. * @client: the client being no longer referenced
  1348. *
  1349. * Must be called when a user of a client is finished with it.
  1350. */
  1351. void i2c_release_client(struct i2c_client *client)
  1352. {
  1353. if (client)
  1354. put_device(&client->dev);
  1355. }
  1356. EXPORT_SYMBOL(i2c_release_client);
  1357. struct i2c_cmd_arg {
  1358. unsigned cmd;
  1359. void *arg;
  1360. };
  1361. static int i2c_cmd(struct device *dev, void *_arg)
  1362. {
  1363. struct i2c_client *client = i2c_verify_client(dev);
  1364. struct i2c_cmd_arg *arg = _arg;
  1365. struct i2c_driver *driver;
  1366. if (!client || !client->dev.driver)
  1367. return 0;
  1368. driver = to_i2c_driver(client->dev.driver);
  1369. if (driver->command)
  1370. driver->command(client, arg->cmd, arg->arg);
  1371. return 0;
  1372. }
  1373. void i2c_clients_command(struct i2c_adapter *adap, unsigned int cmd, void *arg)
  1374. {
  1375. struct i2c_cmd_arg cmd_arg;
  1376. cmd_arg.cmd = cmd;
  1377. cmd_arg.arg = arg;
  1378. device_for_each_child(&adap->dev, &cmd_arg, i2c_cmd);
  1379. }
  1380. EXPORT_SYMBOL(i2c_clients_command);
  1381. static int __init i2c_init(void)
  1382. {
  1383. int retval;
  1384. retval = bus_register(&i2c_bus_type);
  1385. if (retval)
  1386. return retval;
  1387. #ifdef CONFIG_I2C_COMPAT
  1388. i2c_adapter_compat_class = class_compat_register("i2c-adapter");
  1389. if (!i2c_adapter_compat_class) {
  1390. retval = -ENOMEM;
  1391. goto bus_err;
  1392. }
  1393. #endif
  1394. retval = i2c_add_driver(&dummy_driver);
  1395. if (retval)
  1396. goto class_err;
  1397. return 0;
  1398. class_err:
  1399. #ifdef CONFIG_I2C_COMPAT
  1400. class_compat_unregister(i2c_adapter_compat_class);
  1401. bus_err:
  1402. #endif
  1403. bus_unregister(&i2c_bus_type);
  1404. return retval;
  1405. }
  1406. static void __exit i2c_exit(void)
  1407. {
  1408. i2c_del_driver(&dummy_driver);
  1409. #ifdef CONFIG_I2C_COMPAT
  1410. class_compat_unregister(i2c_adapter_compat_class);
  1411. #endif
  1412. bus_unregister(&i2c_bus_type);
  1413. }
  1414. /* We must initialize early, because some subsystems register i2c drivers
  1415. * in subsys_initcall() code, but are linked (and initialized) before i2c.
  1416. */
  1417. postcore_initcall(i2c_init);
  1418. module_exit(i2c_exit);
  1419. /* ----------------------------------------------------
  1420. * the functional interface to the i2c busses.
  1421. * ----------------------------------------------------
  1422. */
  1423. /**
  1424. * __i2c_transfer - unlocked flavor of i2c_transfer
  1425. * @adap: Handle to I2C bus
  1426. * @msgs: One or more messages to execute before STOP is issued to
  1427. * terminate the operation; each message begins with a START.
  1428. * @num: Number of messages to be executed.
  1429. *
  1430. * Returns negative errno, else the number of messages executed.
  1431. *
  1432. * Adapter lock must be held when calling this function. No debug logging
  1433. * takes place. adap->algo->master_xfer existence isn't checked.
  1434. */
  1435. int __i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
  1436. {
  1437. unsigned long orig_jiffies;
  1438. int ret, try;
  1439. /* Retry automatically on arbitration loss */
  1440. orig_jiffies = jiffies;
  1441. for (ret = 0, try = 0; try <= adap->retries; try++) {
  1442. ret = adap->algo->master_xfer(adap, msgs, num);
  1443. if (ret != -EAGAIN)
  1444. break;
  1445. if (time_after(jiffies, orig_jiffies + adap->timeout))
  1446. break;
  1447. }
  1448. return ret;
  1449. }
  1450. EXPORT_SYMBOL(__i2c_transfer);
  1451. /**
  1452. * i2c_transfer - execute a single or combined I2C message
  1453. * @adap: Handle to I2C bus
  1454. * @msgs: One or more messages to execute before STOP is issued to
  1455. * terminate the operation; each message begins with a START.
  1456. * @num: Number of messages to be executed.
  1457. *
  1458. * Returns negative errno, else the number of messages executed.
  1459. *
  1460. * Note that there is no requirement that each message be sent to
  1461. * the same slave address, although that is the most common model.
  1462. */
  1463. int i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
  1464. {
  1465. int ret;
  1466. /* REVISIT the fault reporting model here is weak:
  1467. *
  1468. * - When we get an error after receiving N bytes from a slave,
  1469. * there is no way to report "N".
  1470. *
  1471. * - When we get a NAK after transmitting N bytes to a slave,
  1472. * there is no way to report "N" ... or to let the master
  1473. * continue executing the rest of this combined message, if
  1474. * that's the appropriate response.
  1475. *
  1476. * - When for example "num" is two and we successfully complete
  1477. * the first message but get an error part way through the
  1478. * second, it's unclear whether that should be reported as
  1479. * one (discarding status on the second message) or errno
  1480. * (discarding status on the first one).
  1481. */
  1482. if (adap->algo->master_xfer) {
  1483. #ifdef DEBUG
  1484. for (ret = 0; ret < num; ret++) {
  1485. dev_dbg(&adap->dev, "master_xfer[%d] %c, addr=0x%02x, "
  1486. "len=%d%s\n", ret, (msgs[ret].flags & I2C_M_RD)
  1487. ? 'R' : 'W', msgs[ret].addr, msgs[ret].len,
  1488. (msgs[ret].flags & I2C_M_RECV_LEN) ? "+" : "");
  1489. }
  1490. #endif
  1491. if (in_atomic() || irqs_disabled()) {
  1492. ret = i2c_trylock_adapter(adap);
  1493. if (!ret)
  1494. /* I2C activity is ongoing. */
  1495. return -EAGAIN;
  1496. } else {
  1497. i2c_lock_adapter(adap);
  1498. }
  1499. ret = __i2c_transfer(adap, msgs, num);
  1500. i2c_unlock_adapter(adap);
  1501. return ret;
  1502. } else {
  1503. dev_dbg(&adap->dev, "I2C level transfers not supported\n");
  1504. return -EOPNOTSUPP;
  1505. }
  1506. }
  1507. EXPORT_SYMBOL(i2c_transfer);
  1508. /**
  1509. * i2c_master_send - issue a single I2C message in master transmit mode
  1510. * @client: Handle to slave device
  1511. * @buf: Data that will be written to the slave
  1512. * @count: How many bytes to write, must be less than 64k since msg.len is u16
  1513. *
  1514. * Returns negative errno, or else the number of bytes written.
  1515. */
  1516. int i2c_master_send(const struct i2c_client *client, const char *buf, int count)
  1517. {
  1518. int ret;
  1519. struct i2c_adapter *adap = client->adapter;
  1520. struct i2c_msg msg;
  1521. msg.addr = client->addr;
  1522. msg.flags = client->flags & I2C_M_TEN;
  1523. msg.len = count;
  1524. msg.buf = (char *)buf;
  1525. ret = i2c_transfer(adap, &msg, 1);
  1526. /*
  1527. * If everything went ok (i.e. 1 msg transmitted), return #bytes
  1528. * transmitted, else error code.
  1529. */
  1530. return (ret == 1) ? count : ret;
  1531. }
  1532. EXPORT_SYMBOL(i2c_master_send);
  1533. /**
  1534. * i2c_master_recv - issue a single I2C message in master receive mode
  1535. * @client: Handle to slave device
  1536. * @buf: Where to store data read from slave
  1537. * @count: How many bytes to read, must be less than 64k since msg.len is u16
  1538. *
  1539. * Returns negative errno, or else the number of bytes read.
  1540. */
  1541. int i2c_master_recv(const struct i2c_client *client, char *buf, int count)
  1542. {
  1543. struct i2c_adapter *adap = client->adapter;
  1544. struct i2c_msg msg;
  1545. int ret;
  1546. msg.addr = client->addr;
  1547. msg.flags = client->flags & I2C_M_TEN;
  1548. msg.flags |= I2C_M_RD;
  1549. msg.len = count;
  1550. msg.buf = buf;
  1551. ret = i2c_transfer(adap, &msg, 1);
  1552. /*
  1553. * If everything went ok (i.e. 1 msg received), return #bytes received,
  1554. * else error code.
  1555. */
  1556. return (ret == 1) ? count : ret;
  1557. }
  1558. EXPORT_SYMBOL(i2c_master_recv);
  1559. /* ----------------------------------------------------
  1560. * the i2c address scanning function
  1561. * Will not work for 10-bit addresses!
  1562. * ----------------------------------------------------
  1563. */
  1564. /*
  1565. * Legacy default probe function, mostly relevant for SMBus. The default
  1566. * probe method is a quick write, but it is known to corrupt the 24RF08
  1567. * EEPROMs due to a state machine bug, and could also irreversibly
  1568. * write-protect some EEPROMs, so for address ranges 0x30-0x37 and 0x50-0x5f,
  1569. * we use a short byte read instead. Also, some bus drivers don't implement
  1570. * quick write, so we fallback to a byte read in that case too.
  1571. * On x86, there is another special case for FSC hardware monitoring chips,
  1572. * which want regular byte reads (address 0x73.) Fortunately, these are the
  1573. * only known chips using this I2C address on PC hardware.
  1574. * Returns 1 if probe succeeded, 0 if not.
  1575. */
  1576. static int i2c_default_probe(struct i2c_adapter *adap, unsigned short addr)
  1577. {
  1578. int err;
  1579. union i2c_smbus_data dummy;
  1580. #ifdef CONFIG_X86
  1581. if (addr == 0x73 && (adap->class & I2C_CLASS_HWMON)
  1582. && i2c_check_functionality(adap, I2C_FUNC_SMBUS_READ_BYTE_DATA))
  1583. err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0,
  1584. I2C_SMBUS_BYTE_DATA, &dummy);
  1585. else
  1586. #endif
  1587. if (!((addr & ~0x07) == 0x30 || (addr & ~0x0f) == 0x50)
  1588. && i2c_check_functionality(adap, I2C_FUNC_SMBUS_QUICK))
  1589. err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_WRITE, 0,
  1590. I2C_SMBUS_QUICK, NULL);
  1591. else if (i2c_check_functionality(adap, I2C_FUNC_SMBUS_READ_BYTE))
  1592. err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0,
  1593. I2C_SMBUS_BYTE, &dummy);
  1594. else {
  1595. dev_warn(&adap->dev, "No suitable probing method supported for address 0x%02X\n",
  1596. addr);
  1597. err = -EOPNOTSUPP;
  1598. }
  1599. return err >= 0;
  1600. }
  1601. static int i2c_detect_address(struct i2c_client *temp_client,
  1602. struct i2c_driver *driver)
  1603. {
  1604. struct i2c_board_info info;
  1605. struct i2c_adapter *adapter = temp_client->adapter;
  1606. int addr = temp_client->addr;
  1607. int err;
  1608. /* Make sure the address is valid */
  1609. err = i2c_check_addr_validity(addr);
  1610. if (err) {
  1611. dev_warn(&adapter->dev, "Invalid probe address 0x%02x\n",
  1612. addr);
  1613. return err;
  1614. }
  1615. /* Skip if already in use */
  1616. if (i2c_check_addr_busy(adapter, addr))
  1617. return 0;
  1618. /* Make sure there is something at this address */
  1619. if (!i2c_default_probe(adapter, addr))
  1620. return 0;
  1621. /* Finally call the custom detection function */
  1622. memset(&info, 0, sizeof(struct i2c_board_info));
  1623. info.addr = addr;
  1624. err = driver->detect(temp_client, &info);
  1625. if (err) {
  1626. /* -ENODEV is returned if the detection fails. We catch it
  1627. here as this isn't an error. */
  1628. return err == -ENODEV ? 0 : err;
  1629. }
  1630. /* Consistency check */
  1631. if (info.type[0] == '\0') {
  1632. dev_err(&adapter->dev, "%s detection function provided "
  1633. "no name for 0x%x\n", driver->driver.name,
  1634. addr);
  1635. } else {
  1636. struct i2c_client *client;
  1637. /* Detection succeeded, instantiate the device */
  1638. dev_dbg(&adapter->dev, "Creating %s at 0x%02x\n",
  1639. info.type, info.addr);
  1640. client = i2c_new_device(adapter, &info);
  1641. if (client)
  1642. list_add_tail(&client->detected, &driver->clients);
  1643. else
  1644. dev_err(&adapter->dev, "Failed creating %s at 0x%02x\n",
  1645. info.type, info.addr);
  1646. }
  1647. return 0;
  1648. }
  1649. static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver)
  1650. {
  1651. const unsigned short *address_list;
  1652. struct i2c_client *temp_client;
  1653. int i, err = 0;
  1654. int adap_id = i2c_adapter_id(adapter);
  1655. address_list = driver->address_list;
  1656. if (!driver->detect || !address_list)
  1657. return 0;
  1658. /* Stop here if the classes do not match */
  1659. if (!(adapter->class & driver->class))
  1660. return 0;
  1661. /* Set up a temporary client to help detect callback */
  1662. temp_client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
  1663. if (!temp_client)
  1664. return -ENOMEM;
  1665. temp_client->adapter = adapter;
  1666. for (i = 0; address_list[i] != I2C_CLIENT_END; i += 1) {
  1667. dev_dbg(&adapter->dev, "found normal entry for adapter %d, "
  1668. "addr 0x%02x\n", adap_id, address_list[i]);
  1669. temp_client->addr = address_list[i];
  1670. err = i2c_detect_address(temp_client, driver);
  1671. if (unlikely(err))
  1672. break;
  1673. }
  1674. kfree(temp_client);
  1675. return err;
  1676. }
  1677. int i2c_probe_func_quick_read(struct i2c_adapter *adap, unsigned short addr)
  1678. {
  1679. return i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0,
  1680. I2C_SMBUS_QUICK, NULL) >= 0;
  1681. }
  1682. EXPORT_SYMBOL_GPL(i2c_probe_func_quick_read);
  1683. struct i2c_client *
  1684. i2c_new_probed_device(struct i2c_adapter *adap,
  1685. struct i2c_board_info *info,
  1686. unsigned short const *addr_list,
  1687. int (*probe)(struct i2c_adapter *, unsigned short addr))
  1688. {
  1689. int i;
  1690. if (!probe)
  1691. probe = i2c_default_probe;
  1692. for (i = 0; addr_list[i] != I2C_CLIENT_END; i++) {
  1693. /* Check address validity */
  1694. if (i2c_check_addr_validity(addr_list[i]) < 0) {
  1695. dev_warn(&adap->dev, "Invalid 7-bit address "
  1696. "0x%02x\n", addr_list[i]);
  1697. continue;
  1698. }
  1699. /* Check address availability */
  1700. if (i2c_check_addr_busy(adap, addr_list[i])) {
  1701. dev_dbg(&adap->dev, "Address 0x%02x already in "
  1702. "use, not probing\n", addr_list[i]);
  1703. continue;
  1704. }
  1705. /* Test address responsiveness */
  1706. if (probe(adap, addr_list[i]))
  1707. break;
  1708. }
  1709. if (addr_list[i] == I2C_CLIENT_END) {
  1710. dev_dbg(&adap->dev, "Probing failed, no device found\n");
  1711. return NULL;
  1712. }
  1713. info->addr = addr_list[i];
  1714. return i2c_new_device(adap, info);
  1715. }
  1716. EXPORT_SYMBOL_GPL(i2c_new_probed_device);
  1717. struct i2c_adapter *i2c_get_adapter(int nr)
  1718. {
  1719. struct i2c_adapter *adapter;
  1720. mutex_lock(&core_lock);
  1721. adapter = idr_find(&i2c_adapter_idr, nr);
  1722. if (adapter && !try_module_get(adapter->owner))
  1723. adapter = NULL;
  1724. mutex_unlock(&core_lock);
  1725. return adapter;
  1726. }
  1727. EXPORT_SYMBOL(i2c_get_adapter);
  1728. void i2c_put_adapter(struct i2c_adapter *adap)
  1729. {
  1730. if (adap)
  1731. module_put(adap->owner);
  1732. }
  1733. EXPORT_SYMBOL(i2c_put_adapter);
  1734. /* The SMBus parts */
  1735. #define POLY (0x1070U << 3)
  1736. static u8 crc8(u16 data)
  1737. {
  1738. int i;
  1739. for (i = 0; i < 8; i++) {
  1740. if (data & 0x8000)
  1741. data = data ^ POLY;
  1742. data = data << 1;
  1743. }
  1744. return (u8)(data >> 8);
  1745. }
  1746. /* Incremental CRC8 over count bytes in the array pointed to by p */
  1747. static u8 i2c_smbus_pec(u8 crc, u8 *p, size_t count)
  1748. {
  1749. int i;
  1750. for (i = 0; i < count; i++)
  1751. crc = crc8((crc ^ p[i]) << 8);
  1752. return crc;
  1753. }
  1754. /* Assume a 7-bit address, which is reasonable for SMBus */
  1755. static u8 i2c_smbus_msg_pec(u8 pec, struct i2c_msg *msg)
  1756. {
  1757. /* The address will be sent first */
  1758. u8 addr = (msg->addr << 1) | !!(msg->flags & I2C_M_RD);
  1759. pec = i2c_smbus_pec(pec, &addr, 1);
  1760. /* The data buffer follows */
  1761. return i2c_smbus_pec(pec, msg->buf, msg->len);
  1762. }
  1763. /* Used for write only transactions */
  1764. static inline void i2c_smbus_add_pec(struct i2c_msg *msg)
  1765. {
  1766. msg->buf[msg->len] = i2c_smbus_msg_pec(0, msg);
  1767. msg->len++;
  1768. }
  1769. /* Return <0 on CRC error
  1770. If there was a write before this read (most cases) we need to take the
  1771. partial CRC from the write part into account.
  1772. Note that this function does modify the message (we need to decrease the
  1773. message length to hide the CRC byte from the caller). */
  1774. static int i2c_smbus_check_pec(u8 cpec, struct i2c_msg *msg)
  1775. {
  1776. u8 rpec = msg->buf[--msg->len];
  1777. cpec = i2c_smbus_msg_pec(cpec, msg);
  1778. if (rpec != cpec) {
  1779. pr_debug("i2c-core: Bad PEC 0x%02x vs. 0x%02x\n",
  1780. rpec, cpec);
  1781. return -EBADMSG;
  1782. }
  1783. return 0;
  1784. }
  1785. /**
  1786. * i2c_smbus_read_byte - SMBus "receive byte" protocol
  1787. * @client: Handle to slave device
  1788. *
  1789. * This executes the SMBus "receive byte" protocol, returning negative errno
  1790. * else the byte received from the device.
  1791. */
  1792. s32 i2c_smbus_read_byte(const struct i2c_client *client)
  1793. {
  1794. union i2c_smbus_data data;
  1795. int status;
  1796. status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
  1797. I2C_SMBUS_READ, 0,
  1798. I2C_SMBUS_BYTE, &data);
  1799. return (status < 0) ? status : data.byte;
  1800. }
  1801. EXPORT_SYMBOL(i2c_smbus_read_byte);
  1802. /**
  1803. * i2c_smbus_write_byte - SMBus "send byte" protocol
  1804. * @client: Handle to slave device
  1805. * @value: Byte to be sent
  1806. *
  1807. * This executes the SMBus "send byte" protocol, returning negative errno
  1808. * else zero on success.
  1809. */
  1810. s32 i2c_smbus_write_byte(const struct i2c_client *client, u8 value)
  1811. {
  1812. return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
  1813. I2C_SMBUS_WRITE, value, I2C_SMBUS_BYTE, NULL);
  1814. }
  1815. EXPORT_SYMBOL(i2c_smbus_write_byte);
  1816. /**
  1817. * i2c_smbus_read_byte_data - SMBus "read byte" protocol
  1818. * @client: Handle to slave device
  1819. * @command: Byte interpreted by slave
  1820. *
  1821. * This executes the SMBus "read byte" protocol, returning negative errno
  1822. * else a data byte received from the device.
  1823. */
  1824. s32 i2c_smbus_read_byte_data(const struct i2c_client *client, u8 command)
  1825. {
  1826. union i2c_smbus_data data;
  1827. int status;
  1828. status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
  1829. I2C_SMBUS_READ, command,
  1830. I2C_SMBUS_BYTE_DATA, &data);
  1831. return (status < 0) ? status : data.byte;
  1832. }
  1833. EXPORT_SYMBOL(i2c_smbus_read_byte_data);
  1834. /**
  1835. * i2c_smbus_write_byte_data - SMBus "write byte" protocol
  1836. * @client: Handle to slave device
  1837. * @command: Byte interpreted by slave
  1838. * @value: Byte being written
  1839. *
  1840. * This executes the SMBus "write byte" protocol, returning negative errno
  1841. * else zero on success.
  1842. */
  1843. s32 i2c_smbus_write_byte_data(const struct i2c_client *client, u8 command,
  1844. u8 value)
  1845. {
  1846. union i2c_smbus_data data;
  1847. data.byte = value;
  1848. return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
  1849. I2C_SMBUS_WRITE, command,
  1850. I2C_SMBUS_BYTE_DATA, &data);
  1851. }
  1852. EXPORT_SYMBOL(i2c_smbus_write_byte_data);
  1853. /**
  1854. * i2c_smbus_read_word_data - SMBus "read word" protocol
  1855. * @client: Handle to slave device
  1856. * @command: Byte interpreted by slave
  1857. *
  1858. * This executes the SMBus "read word" protocol, returning negative errno
  1859. * else a 16-bit unsigned "word" received from the device.
  1860. */
  1861. s32 i2c_smbus_read_word_data(const struct i2c_client *client, u8 command)
  1862. {
  1863. union i2c_smbus_data data;
  1864. int status;
  1865. status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
  1866. I2C_SMBUS_READ, command,
  1867. I2C_SMBUS_WORD_DATA, &data);
  1868. return (status < 0) ? status : data.word;
  1869. }
  1870. EXPORT_SYMBOL(i2c_smbus_read_word_data);
  1871. /**
  1872. * i2c_smbus_write_word_data - SMBus "write word" protocol
  1873. * @client: Handle to slave device
  1874. * @command: Byte interpreted by slave
  1875. * @value: 16-bit "word" being written
  1876. *
  1877. * This executes the SMBus "write word" protocol, returning negative errno
  1878. * else zero on success.
  1879. */
  1880. s32 i2c_smbus_write_word_data(const struct i2c_client *client, u8 command,
  1881. u16 value)
  1882. {
  1883. union i2c_smbus_data data;
  1884. data.word = value;
  1885. return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
  1886. I2C_SMBUS_WRITE, command,
  1887. I2C_SMBUS_WORD_DATA, &data);
  1888. }
  1889. EXPORT_SYMBOL(i2c_smbus_write_word_data);
  1890. /**
  1891. * i2c_smbus_read_block_data - SMBus "block read" protocol
  1892. * @client: Handle to slave device
  1893. * @command: Byte interpreted by slave
  1894. * @values: Byte array into which data will be read; big enough to hold
  1895. * the data returned by the slave. SMBus allows at most 32 bytes.
  1896. *
  1897. * This executes the SMBus "block read" protocol, returning negative errno
  1898. * else the number of data bytes in the slave's response.
  1899. *
  1900. * Note that using this function requires that the client's adapter support
  1901. * the I2C_FUNC_SMBUS_READ_BLOCK_DATA functionality. Not all adapter drivers
  1902. * support this; its emulation through I2C messaging relies on a specific
  1903. * mechanism (I2C_M_RECV_LEN) which may not be implemented.
  1904. */
  1905. s32 i2c_smbus_read_block_data(const struct i2c_client *client, u8 command,
  1906. u8 *values)
  1907. {
  1908. union i2c_smbus_data data;
  1909. int status;
  1910. status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
  1911. I2C_SMBUS_READ, command,
  1912. I2C_SMBUS_BLOCK_DATA, &data);
  1913. if (status)
  1914. return status;
  1915. memcpy(values, &data.block[1], data.block[0]);
  1916. return data.block[0];
  1917. }
  1918. EXPORT_SYMBOL(i2c_smbus_read_block_data);
  1919. /**
  1920. * i2c_smbus_write_block_data - SMBus "block write" protocol
  1921. * @client: Handle to slave device
  1922. * @command: Byte interpreted by slave
  1923. * @length: Size of data block; SMBus allows at most 32 bytes
  1924. * @values: Byte array which will be written.
  1925. *
  1926. * This executes the SMBus "block write" protocol, returning negative errno
  1927. * else zero on success.
  1928. */
  1929. s32 i2c_smbus_write_block_data(const struct i2c_client *client, u8 command,
  1930. u8 length, const u8 *values)
  1931. {
  1932. union i2c_smbus_data data;
  1933. if (length > I2C_SMBUS_BLOCK_MAX)
  1934. length = I2C_SMBUS_BLOCK_MAX;
  1935. data.block[0] = length;
  1936. memcpy(&data.block[1], values, length);
  1937. return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
  1938. I2C_SMBUS_WRITE, command,
  1939. I2C_SMBUS_BLOCK_DATA, &data);
  1940. }
  1941. EXPORT_SYMBOL(i2c_smbus_write_block_data);
  1942. /* Returns the number of read bytes */
  1943. s32 i2c_smbus_read_i2c_block_data(const struct i2c_client *client, u8 command,
  1944. u8 length, u8 *values)
  1945. {
  1946. union i2c_smbus_data data;
  1947. int status;
  1948. if (length > I2C_SMBUS_BLOCK_MAX)
  1949. length = I2C_SMBUS_BLOCK_MAX;
  1950. data.block[0] = length;
  1951. status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
  1952. I2C_SMBUS_READ, command,
  1953. I2C_SMBUS_I2C_BLOCK_DATA, &data);
  1954. if (status < 0)
  1955. return status;
  1956. memcpy(values, &data.block[1], data.block[0]);
  1957. return data.block[0];
  1958. }
  1959. EXPORT_SYMBOL(i2c_smbus_read_i2c_block_data);
  1960. s32 i2c_smbus_write_i2c_block_data(const struct i2c_client *client, u8 command,
  1961. u8 length, const u8 *values)
  1962. {
  1963. union i2c_smbus_data data;
  1964. if (length > I2C_SMBUS_BLOCK_MAX)
  1965. length = I2C_SMBUS_BLOCK_MAX;
  1966. data.block[0] = length;
  1967. memcpy(data.block + 1, values, length);
  1968. return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
  1969. I2C_SMBUS_WRITE, command,
  1970. I2C_SMBUS_I2C_BLOCK_DATA, &data);
  1971. }
  1972. EXPORT_SYMBOL(i2c_smbus_write_i2c_block_data);
  1973. /* Simulate a SMBus command using the i2c protocol
  1974. No checking of parameters is done! */
  1975. static s32 i2c_smbus_xfer_emulated(struct i2c_adapter *adapter, u16 addr,
  1976. unsigned short flags,
  1977. char read_write, u8 command, int size,
  1978. union i2c_smbus_data *data)
  1979. {
  1980. /* So we need to generate a series of msgs. In the case of writing, we
  1981. need to use only one message; when reading, we need two. We initialize
  1982. most things with sane defaults, to keep the code below somewhat
  1983. simpler. */
  1984. unsigned char msgbuf0[I2C_SMBUS_BLOCK_MAX+3];
  1985. unsigned char msgbuf1[I2C_SMBUS_BLOCK_MAX+2];
  1986. int num = read_write == I2C_SMBUS_READ ? 2 : 1;
  1987. int i;
  1988. u8 partial_pec = 0;
  1989. int status;
  1990. struct i2c_msg msg[2] = {
  1991. {
  1992. .addr = addr,
  1993. .flags = flags,
  1994. .len = 1,
  1995. .buf = msgbuf0,
  1996. }, {
  1997. .addr = addr,
  1998. .flags = flags | I2C_M_RD,
  1999. .len = 0,
  2000. .buf = msgbuf1,
  2001. },
  2002. };
  2003. msgbuf0[0] = command;
  2004. switch (size) {
  2005. case I2C_SMBUS_QUICK:
  2006. msg[0].len = 0;
  2007. /* Special case: The read/write field is used as data */
  2008. msg[0].flags = flags | (read_write == I2C_SMBUS_READ ?
  2009. I2C_M_RD : 0);
  2010. num = 1;
  2011. break;
  2012. case I2C_SMBUS_BYTE:
  2013. if (read_write == I2C_SMBUS_READ) {
  2014. /* Special case: only a read! */
  2015. msg[0].flags = I2C_M_RD | flags;
  2016. num = 1;
  2017. }
  2018. break;
  2019. case I2C_SMBUS_BYTE_DATA:
  2020. if (read_write == I2C_SMBUS_READ)
  2021. msg[1].len = 1;
  2022. else {
  2023. msg[0].len = 2;
  2024. msgbuf0[1] = data->byte;
  2025. }
  2026. break;
  2027. case I2C_SMBUS_WORD_DATA:
  2028. if (read_write == I2C_SMBUS_READ)
  2029. msg[1].len = 2;
  2030. else {
  2031. msg[0].len = 3;
  2032. msgbuf0[1] = data->word & 0xff;
  2033. msgbuf0[2] = data->word >> 8;
  2034. }
  2035. break;
  2036. case I2C_SMBUS_PROC_CALL:
  2037. num = 2; /* Special case */
  2038. read_write = I2C_SMBUS_READ;
  2039. msg[0].len = 3;
  2040. msg[1].len = 2;
  2041. msgbuf0[1] = data->word & 0xff;
  2042. msgbuf0[2] = data->word >> 8;
  2043. break;
  2044. case I2C_SMBUS_BLOCK_DATA:
  2045. if (read_write == I2C_SMBUS_READ) {
  2046. msg[1].flags |= I2C_M_RECV_LEN;
  2047. msg[1].len = 1; /* block length will be added by
  2048. the underlying bus driver */
  2049. } else {
  2050. msg[0].len = data->block[0] + 2;
  2051. if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 2) {
  2052. dev_err(&adapter->dev,
  2053. "Invalid block write size %d\n",
  2054. data->block[0]);
  2055. return -EINVAL;
  2056. }
  2057. for (i = 1; i < msg[0].len; i++)
  2058. msgbuf0[i] = data->block[i-1];
  2059. }
  2060. break;
  2061. case I2C_SMBUS_BLOCK_PROC_CALL:
  2062. num = 2; /* Another special case */
  2063. read_write = I2C_SMBUS_READ;
  2064. if (data->block[0] > I2C_SMBUS_BLOCK_MAX) {
  2065. dev_err(&adapter->dev,
  2066. "Invalid block write size %d\n",
  2067. data->block[0]);
  2068. return -EINVAL;
  2069. }
  2070. msg[0].len = data->block[0] + 2;
  2071. for (i = 1; i < msg[0].len; i++)
  2072. msgbuf0[i] = data->block[i-1];
  2073. msg[1].flags |= I2C_M_RECV_LEN;
  2074. msg[1].len = 1; /* block length will be added by
  2075. the underlying bus driver */
  2076. break;
  2077. case I2C_SMBUS_I2C_BLOCK_DATA:
  2078. if (read_write == I2C_SMBUS_READ) {
  2079. msg[1].len = data->block[0];
  2080. } else {
  2081. msg[0].len = data->block[0] + 1;
  2082. if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 1) {
  2083. dev_err(&adapter->dev,
  2084. "Invalid block write size %d\n",
  2085. data->block[0]);
  2086. return -EINVAL;
  2087. }
  2088. for (i = 1; i <= data->block[0]; i++)
  2089. msgbuf0[i] = data->block[i];
  2090. }
  2091. break;
  2092. default:
  2093. dev_err(&adapter->dev, "Unsupported transaction %d\n", size);
  2094. return -EOPNOTSUPP;
  2095. }
  2096. i = ((flags & I2C_CLIENT_PEC) && size != I2C_SMBUS_QUICK
  2097. && size != I2C_SMBUS_I2C_BLOCK_DATA);
  2098. if (i) {
  2099. /* Compute PEC if first message is a write */
  2100. if (!(msg[0].flags & I2C_M_RD)) {
  2101. if (num == 1) /* Write only */
  2102. i2c_smbus_add_pec(&msg[0]);
  2103. else /* Write followed by read */
  2104. partial_pec = i2c_smbus_msg_pec(0, &msg[0]);
  2105. }
  2106. /* Ask for PEC if last message is a read */
  2107. if (msg[num-1].flags & I2C_M_RD)
  2108. msg[num-1].len++;
  2109. }
  2110. status = i2c_transfer(adapter, msg, num);
  2111. if (status < 0)
  2112. return status;
  2113. /* Check PEC if last message is a read */
  2114. if (i && (msg[num-1].flags & I2C_M_RD)) {
  2115. status = i2c_smbus_check_pec(partial_pec, &msg[num-1]);
  2116. if (status < 0)
  2117. return status;
  2118. }
  2119. if (read_write == I2C_SMBUS_READ)
  2120. switch (size) {
  2121. case I2C_SMBUS_BYTE:
  2122. data->byte = msgbuf0[0];
  2123. break;
  2124. case I2C_SMBUS_BYTE_DATA:
  2125. data->byte = msgbuf1[0];
  2126. break;
  2127. case I2C_SMBUS_WORD_DATA:
  2128. case I2C_SMBUS_PROC_CALL:
  2129. data->word = msgbuf1[0] | (msgbuf1[1] << 8);
  2130. break;
  2131. case I2C_SMBUS_I2C_BLOCK_DATA:
  2132. for (i = 0; i < data->block[0]; i++)
  2133. data->block[i+1] = msgbuf1[i];
  2134. break;
  2135. case I2C_SMBUS_BLOCK_DATA:
  2136. case I2C_SMBUS_BLOCK_PROC_CALL:
  2137. for (i = 0; i < msgbuf1[0] + 1; i++)
  2138. data->block[i] = msgbuf1[i];
  2139. break;
  2140. }
  2141. return 0;
  2142. }
  2143. /**
  2144. * i2c_smbus_xfer - execute SMBus protocol operations
  2145. * @adapter: Handle to I2C bus
  2146. * @addr: Address of SMBus slave on that bus
  2147. * @flags: I2C_CLIENT_* flags (usually zero or I2C_CLIENT_PEC)
  2148. * @read_write: I2C_SMBUS_READ or I2C_SMBUS_WRITE
  2149. * @command: Byte interpreted by slave, for protocols which use such bytes
  2150. * @protocol: SMBus protocol operation to execute, such as I2C_SMBUS_PROC_CALL
  2151. * @data: Data to be read or written
  2152. *
  2153. * This executes an SMBus protocol operation, and returns a negative
  2154. * errno code else zero on success.
  2155. */
  2156. s32 i2c_smbus_xfer(struct i2c_adapter *adapter, u16 addr, unsigned short flags,
  2157. char read_write, u8 command, int protocol,
  2158. union i2c_smbus_data *data)
  2159. {
  2160. unsigned long orig_jiffies;
  2161. int try;
  2162. s32 res;
  2163. flags &= I2C_M_TEN | I2C_CLIENT_PEC | I2C_CLIENT_SCCB;
  2164. if (adapter->algo->smbus_xfer) {
  2165. i2c_lock_adapter(adapter);
  2166. /* Retry automatically on arbitration loss */
  2167. orig_jiffies = jiffies;
  2168. for (res = 0, try = 0; try <= adapter->retries; try++) {
  2169. res = adapter->algo->smbus_xfer(adapter, addr, flags,
  2170. read_write, command,
  2171. protocol, data);
  2172. if (res != -EAGAIN)
  2173. break;
  2174. if (time_after(jiffies,
  2175. orig_jiffies + adapter->timeout))
  2176. break;
  2177. }
  2178. i2c_unlock_adapter(adapter);
  2179. if (res != -EOPNOTSUPP || !adapter->algo->master_xfer)
  2180. return res;
  2181. /*
  2182. * Fall back to i2c_smbus_xfer_emulated if the adapter doesn't
  2183. * implement native support for the SMBus operation.
  2184. */
  2185. }
  2186. return i2c_smbus_xfer_emulated(adapter, addr, flags, read_write,
  2187. command, protocol, data);
  2188. }
  2189. EXPORT_SYMBOL(i2c_smbus_xfer);
  2190. MODULE_AUTHOR("Simon G. Vogl <simon@tk.uni-linz.ac.at>");
  2191. MODULE_DESCRIPTION("I2C-Bus main module");
  2192. MODULE_LICENSE("GPL");