i2c-core.c 68 KB

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