i2c-core.c 66 KB

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