i2c-core.c 56 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070
  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., 675 Mass Ave, Cambridge, MA 02139, USA. */
  15. /* ------------------------------------------------------------------------- */
  16. /* With some changes from Kyösti Mälkki <kmalkki@cc.hut.fi>.
  17. All SMBus-related things are written by Frodo Looijaard <frodol@dds.nl>
  18. SMBus 2.0 support by Mark Studebaker <mdsxyz123@yahoo.com> and
  19. Jean Delvare <khali@linux-fr.org> */
  20. #include <linux/module.h>
  21. #include <linux/kernel.h>
  22. #include <linux/errno.h>
  23. #include <linux/slab.h>
  24. #include <linux/i2c.h>
  25. #include <linux/init.h>
  26. #include <linux/idr.h>
  27. #include <linux/mutex.h>
  28. #include <linux/of_i2c.h>
  29. #include <linux/of_device.h>
  30. #include <linux/completion.h>
  31. #include <linux/hardirq.h>
  32. #include <linux/irqflags.h>
  33. #include <linux/rwsem.h>
  34. #include <linux/pm_runtime.h>
  35. #include <asm/uaccess.h>
  36. #include "i2c-core.h"
  37. /* core_lock protects i2c_adapter_idr, and guarantees
  38. that device detection, deletion of detected devices, and attach_adapter
  39. and detach_adapter calls are serialized */
  40. static DEFINE_MUTEX(core_lock);
  41. static DEFINE_IDR(i2c_adapter_idr);
  42. static struct device_type i2c_client_type;
  43. static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver);
  44. /* ------------------------------------------------------------------------- */
  45. static const struct i2c_device_id *i2c_match_id(const struct i2c_device_id *id,
  46. const struct i2c_client *client)
  47. {
  48. while (id->name[0]) {
  49. if (strcmp(client->name, id->name) == 0)
  50. return id;
  51. id++;
  52. }
  53. return NULL;
  54. }
  55. static int i2c_device_match(struct device *dev, struct device_driver *drv)
  56. {
  57. struct i2c_client *client = i2c_verify_client(dev);
  58. struct i2c_driver *driver;
  59. if (!client)
  60. return 0;
  61. /* Attempt an OF style match */
  62. if (of_driver_match_device(dev, drv))
  63. return 1;
  64. driver = to_i2c_driver(drv);
  65. /* match on an id table if there is one */
  66. if (driver->id_table)
  67. return i2c_match_id(driver->id_table, client) != NULL;
  68. return 0;
  69. }
  70. #ifdef CONFIG_HOTPLUG
  71. /* uevent helps with hotplug: modprobe -q $(MODALIAS) */
  72. static int i2c_device_uevent(struct device *dev, struct kobj_uevent_env *env)
  73. {
  74. struct i2c_client *client = to_i2c_client(dev);
  75. if (add_uevent_var(env, "MODALIAS=%s%s",
  76. I2C_MODULE_PREFIX, client->name))
  77. return -ENOMEM;
  78. dev_dbg(dev, "uevent\n");
  79. return 0;
  80. }
  81. #else
  82. #define i2c_device_uevent NULL
  83. #endif /* CONFIG_HOTPLUG */
  84. static int i2c_device_probe(struct device *dev)
  85. {
  86. struct i2c_client *client = i2c_verify_client(dev);
  87. struct i2c_driver *driver;
  88. int status;
  89. if (!client)
  90. return 0;
  91. driver = to_i2c_driver(dev->driver);
  92. if (!driver->probe || !driver->id_table)
  93. return -ENODEV;
  94. client->driver = driver;
  95. if (!device_can_wakeup(&client->dev))
  96. device_init_wakeup(&client->dev,
  97. client->flags & I2C_CLIENT_WAKE);
  98. dev_dbg(dev, "probe\n");
  99. status = driver->probe(client, i2c_match_id(driver->id_table, client));
  100. if (status) {
  101. client->driver = NULL;
  102. i2c_set_clientdata(client, NULL);
  103. }
  104. return status;
  105. }
  106. static int i2c_device_remove(struct device *dev)
  107. {
  108. struct i2c_client *client = i2c_verify_client(dev);
  109. struct i2c_driver *driver;
  110. int status;
  111. if (!client || !dev->driver)
  112. return 0;
  113. driver = to_i2c_driver(dev->driver);
  114. if (driver->remove) {
  115. dev_dbg(dev, "remove\n");
  116. status = driver->remove(client);
  117. } else {
  118. dev->driver = NULL;
  119. status = 0;
  120. }
  121. if (status == 0) {
  122. client->driver = NULL;
  123. i2c_set_clientdata(client, NULL);
  124. }
  125. return status;
  126. }
  127. static void i2c_device_shutdown(struct device *dev)
  128. {
  129. struct i2c_client *client = i2c_verify_client(dev);
  130. struct i2c_driver *driver;
  131. if (!client || !dev->driver)
  132. return;
  133. driver = to_i2c_driver(dev->driver);
  134. if (driver->shutdown)
  135. driver->shutdown(client);
  136. }
  137. #ifdef CONFIG_PM_SLEEP
  138. static int i2c_legacy_suspend(struct device *dev, pm_message_t mesg)
  139. {
  140. struct i2c_client *client = i2c_verify_client(dev);
  141. struct i2c_driver *driver;
  142. if (!client || !dev->driver)
  143. return 0;
  144. driver = to_i2c_driver(dev->driver);
  145. if (!driver->suspend)
  146. return 0;
  147. return driver->suspend(client, mesg);
  148. }
  149. static int i2c_legacy_resume(struct device *dev)
  150. {
  151. struct i2c_client *client = i2c_verify_client(dev);
  152. struct i2c_driver *driver;
  153. if (!client || !dev->driver)
  154. return 0;
  155. driver = to_i2c_driver(dev->driver);
  156. if (!driver->resume)
  157. return 0;
  158. return driver->resume(client);
  159. }
  160. static int i2c_device_pm_suspend(struct device *dev)
  161. {
  162. const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
  163. if (pm_runtime_suspended(dev))
  164. return 0;
  165. if (pm)
  166. return pm->suspend ? pm->suspend(dev) : 0;
  167. return i2c_legacy_suspend(dev, PMSG_SUSPEND);
  168. }
  169. static int i2c_device_pm_resume(struct device *dev)
  170. {
  171. const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
  172. int ret;
  173. if (pm)
  174. ret = pm->resume ? pm->resume(dev) : 0;
  175. else
  176. ret = i2c_legacy_resume(dev);
  177. if (!ret) {
  178. pm_runtime_disable(dev);
  179. pm_runtime_set_active(dev);
  180. pm_runtime_enable(dev);
  181. }
  182. return ret;
  183. }
  184. static int i2c_device_pm_freeze(struct device *dev)
  185. {
  186. const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
  187. if (pm_runtime_suspended(dev))
  188. return 0;
  189. if (pm)
  190. return pm->freeze ? pm->freeze(dev) : 0;
  191. return i2c_legacy_suspend(dev, PMSG_FREEZE);
  192. }
  193. static int i2c_device_pm_thaw(struct device *dev)
  194. {
  195. const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
  196. if (pm_runtime_suspended(dev))
  197. return 0;
  198. if (pm)
  199. return pm->thaw ? pm->thaw(dev) : 0;
  200. return i2c_legacy_resume(dev);
  201. }
  202. static int i2c_device_pm_poweroff(struct device *dev)
  203. {
  204. const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
  205. if (pm_runtime_suspended(dev))
  206. return 0;
  207. if (pm)
  208. return pm->poweroff ? pm->poweroff(dev) : 0;
  209. return i2c_legacy_suspend(dev, PMSG_HIBERNATE);
  210. }
  211. static int i2c_device_pm_restore(struct device *dev)
  212. {
  213. const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
  214. int ret;
  215. if (pm)
  216. ret = pm->restore ? pm->restore(dev) : 0;
  217. else
  218. ret = i2c_legacy_resume(dev);
  219. if (!ret) {
  220. pm_runtime_disable(dev);
  221. pm_runtime_set_active(dev);
  222. pm_runtime_enable(dev);
  223. }
  224. return ret;
  225. }
  226. #else /* !CONFIG_PM_SLEEP */
  227. #define i2c_device_pm_suspend NULL
  228. #define i2c_device_pm_resume NULL
  229. #define i2c_device_pm_freeze NULL
  230. #define i2c_device_pm_thaw NULL
  231. #define i2c_device_pm_poweroff NULL
  232. #define i2c_device_pm_restore NULL
  233. #endif /* !CONFIG_PM_SLEEP */
  234. static void i2c_client_dev_release(struct device *dev)
  235. {
  236. kfree(to_i2c_client(dev));
  237. }
  238. static ssize_t
  239. show_name(struct device *dev, struct device_attribute *attr, char *buf)
  240. {
  241. return sprintf(buf, "%s\n", dev->type == &i2c_client_type ?
  242. to_i2c_client(dev)->name : to_i2c_adapter(dev)->name);
  243. }
  244. static ssize_t
  245. show_modalias(struct device *dev, struct device_attribute *attr, char *buf)
  246. {
  247. struct i2c_client *client = to_i2c_client(dev);
  248. return sprintf(buf, "%s%s\n", I2C_MODULE_PREFIX, client->name);
  249. }
  250. static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
  251. static DEVICE_ATTR(modalias, S_IRUGO, show_modalias, NULL);
  252. static struct attribute *i2c_dev_attrs[] = {
  253. &dev_attr_name.attr,
  254. /* modalias helps coldplug: modprobe $(cat .../modalias) */
  255. &dev_attr_modalias.attr,
  256. NULL
  257. };
  258. static struct attribute_group i2c_dev_attr_group = {
  259. .attrs = i2c_dev_attrs,
  260. };
  261. static const struct attribute_group *i2c_dev_attr_groups[] = {
  262. &i2c_dev_attr_group,
  263. NULL
  264. };
  265. static const struct dev_pm_ops i2c_device_pm_ops = {
  266. .suspend = i2c_device_pm_suspend,
  267. .resume = i2c_device_pm_resume,
  268. .freeze = i2c_device_pm_freeze,
  269. .thaw = i2c_device_pm_thaw,
  270. .poweroff = i2c_device_pm_poweroff,
  271. .restore = i2c_device_pm_restore,
  272. SET_RUNTIME_PM_OPS(
  273. pm_generic_runtime_suspend,
  274. pm_generic_runtime_resume,
  275. pm_generic_runtime_idle
  276. )
  277. };
  278. struct bus_type i2c_bus_type = {
  279. .name = "i2c",
  280. .match = i2c_device_match,
  281. .probe = i2c_device_probe,
  282. .remove = i2c_device_remove,
  283. .shutdown = i2c_device_shutdown,
  284. .pm = &i2c_device_pm_ops,
  285. };
  286. EXPORT_SYMBOL_GPL(i2c_bus_type);
  287. static struct device_type i2c_client_type = {
  288. .groups = i2c_dev_attr_groups,
  289. .uevent = i2c_device_uevent,
  290. .release = i2c_client_dev_release,
  291. };
  292. /**
  293. * i2c_verify_client - return parameter as i2c_client, or NULL
  294. * @dev: device, probably from some driver model iterator
  295. *
  296. * When traversing the driver model tree, perhaps using driver model
  297. * iterators like @device_for_each_child(), you can't assume very much
  298. * about the nodes you find. Use this function to avoid oopses caused
  299. * by wrongly treating some non-I2C device as an i2c_client.
  300. */
  301. struct i2c_client *i2c_verify_client(struct device *dev)
  302. {
  303. return (dev->type == &i2c_client_type)
  304. ? to_i2c_client(dev)
  305. : NULL;
  306. }
  307. EXPORT_SYMBOL(i2c_verify_client);
  308. /* This is a permissive address validity check, I2C address map constraints
  309. * are purposedly not enforced, except for the general call address. */
  310. static int i2c_check_client_addr_validity(const struct i2c_client *client)
  311. {
  312. if (client->flags & I2C_CLIENT_TEN) {
  313. /* 10-bit address, all values are valid */
  314. if (client->addr > 0x3ff)
  315. return -EINVAL;
  316. } else {
  317. /* 7-bit address, reject the general call address */
  318. if (client->addr == 0x00 || client->addr > 0x7f)
  319. return -EINVAL;
  320. }
  321. return 0;
  322. }
  323. /* And this is a strict address validity check, used when probing. If a
  324. * device uses a reserved address, then it shouldn't be probed. 7-bit
  325. * addressing is assumed, 10-bit address devices are rare and should be
  326. * explicitly enumerated. */
  327. static int i2c_check_addr_validity(unsigned short addr)
  328. {
  329. /*
  330. * Reserved addresses per I2C specification:
  331. * 0x00 General call address / START byte
  332. * 0x01 CBUS address
  333. * 0x02 Reserved for different bus format
  334. * 0x03 Reserved for future purposes
  335. * 0x04-0x07 Hs-mode master code
  336. * 0x78-0x7b 10-bit slave addressing
  337. * 0x7c-0x7f Reserved for future purposes
  338. */
  339. if (addr < 0x08 || addr > 0x77)
  340. return -EINVAL;
  341. return 0;
  342. }
  343. static int __i2c_check_addr_busy(struct device *dev, void *addrp)
  344. {
  345. struct i2c_client *client = i2c_verify_client(dev);
  346. int addr = *(int *)addrp;
  347. if (client && client->addr == addr)
  348. return -EBUSY;
  349. return 0;
  350. }
  351. static int i2c_check_addr_busy(struct i2c_adapter *adapter, int addr)
  352. {
  353. return device_for_each_child(&adapter->dev, &addr,
  354. __i2c_check_addr_busy);
  355. }
  356. /**
  357. * i2c_lock_adapter - Get exclusive access to an I2C bus segment
  358. * @adapter: Target I2C bus segment
  359. */
  360. void i2c_lock_adapter(struct i2c_adapter *adapter)
  361. {
  362. rt_mutex_lock(&adapter->bus_lock);
  363. }
  364. EXPORT_SYMBOL_GPL(i2c_lock_adapter);
  365. /**
  366. * i2c_trylock_adapter - Try to get exclusive access to an I2C bus segment
  367. * @adapter: Target I2C bus segment
  368. */
  369. static int i2c_trylock_adapter(struct i2c_adapter *adapter)
  370. {
  371. return rt_mutex_trylock(&adapter->bus_lock);
  372. }
  373. /**
  374. * i2c_unlock_adapter - Release exclusive access to an I2C bus segment
  375. * @adapter: Target I2C bus segment
  376. */
  377. void i2c_unlock_adapter(struct i2c_adapter *adapter)
  378. {
  379. rt_mutex_unlock(&adapter->bus_lock);
  380. }
  381. EXPORT_SYMBOL_GPL(i2c_unlock_adapter);
  382. /**
  383. * i2c_new_device - instantiate an i2c device
  384. * @adap: the adapter managing the device
  385. * @info: describes one I2C device; bus_num is ignored
  386. * Context: can sleep
  387. *
  388. * Create an i2c device. Binding is handled through driver model
  389. * probe()/remove() methods. A driver may be bound to this device when we
  390. * return from this function, or any later moment (e.g. maybe hotplugging will
  391. * load the driver module). This call is not appropriate for use by mainboard
  392. * initialization logic, which usually runs during an arch_initcall() long
  393. * before any i2c_adapter could exist.
  394. *
  395. * This returns the new i2c client, which may be saved for later use with
  396. * i2c_unregister_device(); or NULL to indicate an error.
  397. */
  398. struct i2c_client *
  399. i2c_new_device(struct i2c_adapter *adap, struct i2c_board_info const *info)
  400. {
  401. struct i2c_client *client;
  402. int status;
  403. client = kzalloc(sizeof *client, GFP_KERNEL);
  404. if (!client)
  405. return NULL;
  406. client->adapter = adap;
  407. client->dev.platform_data = info->platform_data;
  408. if (info->archdata)
  409. client->dev.archdata = *info->archdata;
  410. client->flags = info->flags;
  411. client->addr = info->addr;
  412. client->irq = info->irq;
  413. strlcpy(client->name, info->type, sizeof(client->name));
  414. /* Check for address validity */
  415. status = i2c_check_client_addr_validity(client);
  416. if (status) {
  417. dev_err(&adap->dev, "Invalid %d-bit I2C address 0x%02hx\n",
  418. client->flags & I2C_CLIENT_TEN ? 10 : 7, client->addr);
  419. goto out_err_silent;
  420. }
  421. /* Check for address business */
  422. status = i2c_check_addr_busy(adap, client->addr);
  423. if (status)
  424. goto out_err;
  425. client->dev.parent = &client->adapter->dev;
  426. client->dev.bus = &i2c_bus_type;
  427. client->dev.type = &i2c_client_type;
  428. #ifdef CONFIG_OF
  429. client->dev.of_node = info->of_node;
  430. #endif
  431. dev_set_name(&client->dev, "%d-%04x", i2c_adapter_id(adap),
  432. client->addr);
  433. status = device_register(&client->dev);
  434. if (status)
  435. goto out_err;
  436. dev_dbg(&adap->dev, "client [%s] registered with bus id %s\n",
  437. client->name, dev_name(&client->dev));
  438. return client;
  439. out_err:
  440. dev_err(&adap->dev, "Failed to register i2c client %s at 0x%02x "
  441. "(%d)\n", client->name, client->addr, status);
  442. out_err_silent:
  443. kfree(client);
  444. return NULL;
  445. }
  446. EXPORT_SYMBOL_GPL(i2c_new_device);
  447. /**
  448. * i2c_unregister_device - reverse effect of i2c_new_device()
  449. * @client: value returned from i2c_new_device()
  450. * Context: can sleep
  451. */
  452. void i2c_unregister_device(struct i2c_client *client)
  453. {
  454. device_unregister(&client->dev);
  455. }
  456. EXPORT_SYMBOL_GPL(i2c_unregister_device);
  457. static const struct i2c_device_id dummy_id[] = {
  458. { "dummy", 0 },
  459. { },
  460. };
  461. static int dummy_probe(struct i2c_client *client,
  462. const struct i2c_device_id *id)
  463. {
  464. return 0;
  465. }
  466. static int dummy_remove(struct i2c_client *client)
  467. {
  468. return 0;
  469. }
  470. static struct i2c_driver dummy_driver = {
  471. .driver.name = "dummy",
  472. .probe = dummy_probe,
  473. .remove = dummy_remove,
  474. .id_table = dummy_id,
  475. };
  476. /**
  477. * i2c_new_dummy - return a new i2c device bound to a dummy driver
  478. * @adapter: the adapter managing the device
  479. * @address: seven bit address to be used
  480. * Context: can sleep
  481. *
  482. * This returns an I2C client bound to the "dummy" driver, intended for use
  483. * with devices that consume multiple addresses. Examples of such chips
  484. * include various EEPROMS (like 24c04 and 24c08 models).
  485. *
  486. * These dummy devices have two main uses. First, most I2C and SMBus calls
  487. * except i2c_transfer() need a client handle; the dummy will be that handle.
  488. * And second, this prevents the specified address from being bound to a
  489. * different driver.
  490. *
  491. * This returns the new i2c client, which should be saved for later use with
  492. * i2c_unregister_device(); or NULL to indicate an error.
  493. */
  494. struct i2c_client *i2c_new_dummy(struct i2c_adapter *adapter, u16 address)
  495. {
  496. struct i2c_board_info info = {
  497. I2C_BOARD_INFO("dummy", address),
  498. };
  499. return i2c_new_device(adapter, &info);
  500. }
  501. EXPORT_SYMBOL_GPL(i2c_new_dummy);
  502. /* ------------------------------------------------------------------------- */
  503. /* I2C bus adapters -- one roots each I2C or SMBUS segment */
  504. static void i2c_adapter_dev_release(struct device *dev)
  505. {
  506. struct i2c_adapter *adap = to_i2c_adapter(dev);
  507. complete(&adap->dev_released);
  508. }
  509. /*
  510. * Let users instantiate I2C devices through sysfs. This can be used when
  511. * platform initialization code doesn't contain the proper data for
  512. * whatever reason. Also useful for drivers that do device detection and
  513. * detection fails, either because the device uses an unexpected address,
  514. * or this is a compatible device with different ID register values.
  515. *
  516. * Parameter checking may look overzealous, but we really don't want
  517. * the user to provide incorrect parameters.
  518. */
  519. static ssize_t
  520. i2c_sysfs_new_device(struct device *dev, struct device_attribute *attr,
  521. const char *buf, size_t count)
  522. {
  523. struct i2c_adapter *adap = to_i2c_adapter(dev);
  524. struct i2c_board_info info;
  525. struct i2c_client *client;
  526. char *blank, end;
  527. int res;
  528. dev_warn(dev, "The new_device interface is still experimental "
  529. "and may change in a near future\n");
  530. memset(&info, 0, sizeof(struct i2c_board_info));
  531. blank = strchr(buf, ' ');
  532. if (!blank) {
  533. dev_err(dev, "%s: Missing parameters\n", "new_device");
  534. return -EINVAL;
  535. }
  536. if (blank - buf > I2C_NAME_SIZE - 1) {
  537. dev_err(dev, "%s: Invalid device name\n", "new_device");
  538. return -EINVAL;
  539. }
  540. memcpy(info.type, buf, blank - buf);
  541. /* Parse remaining parameters, reject extra parameters */
  542. res = sscanf(++blank, "%hi%c", &info.addr, &end);
  543. if (res < 1) {
  544. dev_err(dev, "%s: Can't parse I2C address\n", "new_device");
  545. return -EINVAL;
  546. }
  547. if (res > 1 && end != '\n') {
  548. dev_err(dev, "%s: Extra parameters\n", "new_device");
  549. return -EINVAL;
  550. }
  551. client = i2c_new_device(adap, &info);
  552. if (!client)
  553. return -EINVAL;
  554. /* Keep track of the added device */
  555. i2c_lock_adapter(adap);
  556. list_add_tail(&client->detected, &adap->userspace_clients);
  557. i2c_unlock_adapter(adap);
  558. dev_info(dev, "%s: Instantiated device %s at 0x%02hx\n", "new_device",
  559. info.type, info.addr);
  560. return count;
  561. }
  562. /*
  563. * And of course let the users delete the devices they instantiated, if
  564. * they got it wrong. This interface can only be used to delete devices
  565. * instantiated by i2c_sysfs_new_device above. This guarantees that we
  566. * don't delete devices to which some kernel code still has references.
  567. *
  568. * Parameter checking may look overzealous, but we really don't want
  569. * the user to delete the wrong device.
  570. */
  571. static ssize_t
  572. i2c_sysfs_delete_device(struct device *dev, struct device_attribute *attr,
  573. const char *buf, size_t count)
  574. {
  575. struct i2c_adapter *adap = to_i2c_adapter(dev);
  576. struct i2c_client *client, *next;
  577. unsigned short addr;
  578. char end;
  579. int res;
  580. /* Parse parameters, reject extra parameters */
  581. res = sscanf(buf, "%hi%c", &addr, &end);
  582. if (res < 1) {
  583. dev_err(dev, "%s: Can't parse I2C address\n", "delete_device");
  584. return -EINVAL;
  585. }
  586. if (res > 1 && end != '\n') {
  587. dev_err(dev, "%s: Extra parameters\n", "delete_device");
  588. return -EINVAL;
  589. }
  590. /* Make sure the device was added through sysfs */
  591. res = -ENOENT;
  592. i2c_lock_adapter(adap);
  593. list_for_each_entry_safe(client, next, &adap->userspace_clients,
  594. detected) {
  595. if (client->addr == addr) {
  596. dev_info(dev, "%s: Deleting device %s at 0x%02hx\n",
  597. "delete_device", client->name, client->addr);
  598. list_del(&client->detected);
  599. i2c_unregister_device(client);
  600. res = count;
  601. break;
  602. }
  603. }
  604. i2c_unlock_adapter(adap);
  605. if (res < 0)
  606. dev_err(dev, "%s: Can't find device in list\n",
  607. "delete_device");
  608. return res;
  609. }
  610. static DEVICE_ATTR(new_device, S_IWUSR, NULL, i2c_sysfs_new_device);
  611. static DEVICE_ATTR(delete_device, S_IWUSR, NULL, i2c_sysfs_delete_device);
  612. static struct attribute *i2c_adapter_attrs[] = {
  613. &dev_attr_name.attr,
  614. &dev_attr_new_device.attr,
  615. &dev_attr_delete_device.attr,
  616. NULL
  617. };
  618. static struct attribute_group i2c_adapter_attr_group = {
  619. .attrs = i2c_adapter_attrs,
  620. };
  621. static const struct attribute_group *i2c_adapter_attr_groups[] = {
  622. &i2c_adapter_attr_group,
  623. NULL
  624. };
  625. static struct device_type i2c_adapter_type = {
  626. .groups = i2c_adapter_attr_groups,
  627. .release = i2c_adapter_dev_release,
  628. };
  629. #ifdef CONFIG_I2C_COMPAT
  630. static struct class_compat *i2c_adapter_compat_class;
  631. #endif
  632. static void i2c_scan_static_board_info(struct i2c_adapter *adapter)
  633. {
  634. struct i2c_devinfo *devinfo;
  635. down_read(&__i2c_board_lock);
  636. list_for_each_entry(devinfo, &__i2c_board_list, list) {
  637. if (devinfo->busnum == adapter->nr
  638. && !i2c_new_device(adapter,
  639. &devinfo->board_info))
  640. dev_err(&adapter->dev,
  641. "Can't create device at 0x%02x\n",
  642. devinfo->board_info.addr);
  643. }
  644. up_read(&__i2c_board_lock);
  645. }
  646. static int i2c_do_add_adapter(struct i2c_driver *driver,
  647. struct i2c_adapter *adap)
  648. {
  649. /* Detect supported devices on that bus, and instantiate them */
  650. i2c_detect(adap, driver);
  651. /* Let legacy drivers scan this bus for matching devices */
  652. if (driver->attach_adapter) {
  653. /* We ignore the return code; if it fails, too bad */
  654. driver->attach_adapter(adap);
  655. }
  656. return 0;
  657. }
  658. static int __process_new_adapter(struct device_driver *d, void *data)
  659. {
  660. return i2c_do_add_adapter(to_i2c_driver(d), data);
  661. }
  662. static int i2c_register_adapter(struct i2c_adapter *adap)
  663. {
  664. int res = 0, dummy;
  665. /* Can't register until after driver model init */
  666. if (unlikely(WARN_ON(!i2c_bus_type.p))) {
  667. res = -EAGAIN;
  668. goto out_list;
  669. }
  670. rt_mutex_init(&adap->bus_lock);
  671. INIT_LIST_HEAD(&adap->userspace_clients);
  672. /* Set default timeout to 1 second if not already set */
  673. if (adap->timeout == 0)
  674. adap->timeout = HZ;
  675. dev_set_name(&adap->dev, "i2c-%d", adap->nr);
  676. adap->dev.bus = &i2c_bus_type;
  677. adap->dev.type = &i2c_adapter_type;
  678. res = device_register(&adap->dev);
  679. if (res)
  680. goto out_list;
  681. dev_dbg(&adap->dev, "adapter [%s] registered\n", adap->name);
  682. #ifdef CONFIG_I2C_COMPAT
  683. res = class_compat_create_link(i2c_adapter_compat_class, &adap->dev,
  684. adap->dev.parent);
  685. if (res)
  686. dev_warn(&adap->dev,
  687. "Failed to create compatibility class link\n");
  688. #endif
  689. /* create pre-declared device nodes */
  690. if (adap->nr < __i2c_first_dynamic_bus_num)
  691. i2c_scan_static_board_info(adap);
  692. /* Register devices from the device tree */
  693. of_i2c_register_devices(adap);
  694. /* Notify drivers */
  695. mutex_lock(&core_lock);
  696. dummy = bus_for_each_drv(&i2c_bus_type, NULL, adap,
  697. __process_new_adapter);
  698. mutex_unlock(&core_lock);
  699. return 0;
  700. out_list:
  701. mutex_lock(&core_lock);
  702. idr_remove(&i2c_adapter_idr, adap->nr);
  703. mutex_unlock(&core_lock);
  704. return res;
  705. }
  706. /**
  707. * i2c_add_adapter - declare i2c adapter, use dynamic bus number
  708. * @adapter: the adapter to add
  709. * Context: can sleep
  710. *
  711. * This routine is used to declare an I2C adapter when its bus number
  712. * doesn't matter. Examples: for I2C adapters dynamically added by
  713. * USB links or PCI plugin cards.
  714. *
  715. * When this returns zero, a new bus number was allocated and stored
  716. * in adap->nr, and the specified adapter became available for clients.
  717. * Otherwise, a negative errno value is returned.
  718. */
  719. int i2c_add_adapter(struct i2c_adapter *adapter)
  720. {
  721. int id, res = 0;
  722. retry:
  723. if (idr_pre_get(&i2c_adapter_idr, GFP_KERNEL) == 0)
  724. return -ENOMEM;
  725. mutex_lock(&core_lock);
  726. /* "above" here means "above or equal to", sigh */
  727. res = idr_get_new_above(&i2c_adapter_idr, adapter,
  728. __i2c_first_dynamic_bus_num, &id);
  729. mutex_unlock(&core_lock);
  730. if (res < 0) {
  731. if (res == -EAGAIN)
  732. goto retry;
  733. return res;
  734. }
  735. adapter->nr = id;
  736. return i2c_register_adapter(adapter);
  737. }
  738. EXPORT_SYMBOL(i2c_add_adapter);
  739. /**
  740. * i2c_add_numbered_adapter - declare i2c adapter, use static bus number
  741. * @adap: the adapter to register (with adap->nr initialized)
  742. * Context: can sleep
  743. *
  744. * This routine is used to declare an I2C adapter when its bus number
  745. * matters. For example, use it for I2C adapters from system-on-chip CPUs,
  746. * or otherwise built in to the system's mainboard, and where i2c_board_info
  747. * is used to properly configure I2C devices.
  748. *
  749. * If no devices have pre-been declared for this bus, then be sure to
  750. * register the adapter before any dynamically allocated ones. Otherwise
  751. * the required bus ID may not be available.
  752. *
  753. * When this returns zero, the specified adapter became available for
  754. * clients using the bus number provided in adap->nr. Also, the table
  755. * of I2C devices pre-declared using i2c_register_board_info() is scanned,
  756. * and the appropriate driver model device nodes are created. Otherwise, a
  757. * negative errno value is returned.
  758. */
  759. int i2c_add_numbered_adapter(struct i2c_adapter *adap)
  760. {
  761. int id;
  762. int status;
  763. if (adap->nr & ~MAX_ID_MASK)
  764. return -EINVAL;
  765. retry:
  766. if (idr_pre_get(&i2c_adapter_idr, GFP_KERNEL) == 0)
  767. return -ENOMEM;
  768. mutex_lock(&core_lock);
  769. /* "above" here means "above or equal to", sigh;
  770. * we need the "equal to" result to force the result
  771. */
  772. status = idr_get_new_above(&i2c_adapter_idr, adap, adap->nr, &id);
  773. if (status == 0 && id != adap->nr) {
  774. status = -EBUSY;
  775. idr_remove(&i2c_adapter_idr, id);
  776. }
  777. mutex_unlock(&core_lock);
  778. if (status == -EAGAIN)
  779. goto retry;
  780. if (status == 0)
  781. status = i2c_register_adapter(adap);
  782. return status;
  783. }
  784. EXPORT_SYMBOL_GPL(i2c_add_numbered_adapter);
  785. static int i2c_do_del_adapter(struct i2c_driver *driver,
  786. struct i2c_adapter *adapter)
  787. {
  788. struct i2c_client *client, *_n;
  789. int res;
  790. /* Remove the devices we created ourselves as the result of hardware
  791. * probing (using a driver's detect method) */
  792. list_for_each_entry_safe(client, _n, &driver->clients, detected) {
  793. if (client->adapter == adapter) {
  794. dev_dbg(&adapter->dev, "Removing %s at 0x%x\n",
  795. client->name, client->addr);
  796. list_del(&client->detected);
  797. i2c_unregister_device(client);
  798. }
  799. }
  800. if (!driver->detach_adapter)
  801. return 0;
  802. res = driver->detach_adapter(adapter);
  803. if (res)
  804. dev_err(&adapter->dev, "detach_adapter failed (%d) "
  805. "for driver [%s]\n", res, driver->driver.name);
  806. return res;
  807. }
  808. static int __unregister_client(struct device *dev, void *dummy)
  809. {
  810. struct i2c_client *client = i2c_verify_client(dev);
  811. if (client)
  812. i2c_unregister_device(client);
  813. return 0;
  814. }
  815. static int __process_removed_adapter(struct device_driver *d, void *data)
  816. {
  817. return i2c_do_del_adapter(to_i2c_driver(d), data);
  818. }
  819. /**
  820. * i2c_del_adapter - unregister I2C adapter
  821. * @adap: the adapter being unregistered
  822. * Context: can sleep
  823. *
  824. * This unregisters an I2C adapter which was previously registered
  825. * by @i2c_add_adapter or @i2c_add_numbered_adapter.
  826. */
  827. int i2c_del_adapter(struct i2c_adapter *adap)
  828. {
  829. int res = 0;
  830. struct i2c_adapter *found;
  831. struct i2c_client *client, *next;
  832. /* First make sure that this adapter was ever added */
  833. mutex_lock(&core_lock);
  834. found = idr_find(&i2c_adapter_idr, adap->nr);
  835. mutex_unlock(&core_lock);
  836. if (found != adap) {
  837. pr_debug("i2c-core: attempting to delete unregistered "
  838. "adapter [%s]\n", adap->name);
  839. return -EINVAL;
  840. }
  841. /* Tell drivers about this removal */
  842. mutex_lock(&core_lock);
  843. res = bus_for_each_drv(&i2c_bus_type, NULL, adap,
  844. __process_removed_adapter);
  845. mutex_unlock(&core_lock);
  846. if (res)
  847. return res;
  848. /* Remove devices instantiated from sysfs */
  849. i2c_lock_adapter(adap);
  850. list_for_each_entry_safe(client, next, &adap->userspace_clients,
  851. detected) {
  852. dev_dbg(&adap->dev, "Removing %s at 0x%x\n", client->name,
  853. client->addr);
  854. list_del(&client->detected);
  855. i2c_unregister_device(client);
  856. }
  857. i2c_unlock_adapter(adap);
  858. /* Detach any active clients. This can't fail, thus we do not
  859. checking the returned value. */
  860. res = device_for_each_child(&adap->dev, NULL, __unregister_client);
  861. #ifdef CONFIG_I2C_COMPAT
  862. class_compat_remove_link(i2c_adapter_compat_class, &adap->dev,
  863. adap->dev.parent);
  864. #endif
  865. /* device name is gone after device_unregister */
  866. dev_dbg(&adap->dev, "adapter [%s] unregistered\n", adap->name);
  867. /* clean up the sysfs representation */
  868. init_completion(&adap->dev_released);
  869. device_unregister(&adap->dev);
  870. /* wait for sysfs to drop all references */
  871. wait_for_completion(&adap->dev_released);
  872. /* free bus id */
  873. mutex_lock(&core_lock);
  874. idr_remove(&i2c_adapter_idr, adap->nr);
  875. mutex_unlock(&core_lock);
  876. /* Clear the device structure in case this adapter is ever going to be
  877. added again */
  878. memset(&adap->dev, 0, sizeof(adap->dev));
  879. return 0;
  880. }
  881. EXPORT_SYMBOL(i2c_del_adapter);
  882. /* ------------------------------------------------------------------------- */
  883. static int __process_new_driver(struct device *dev, void *data)
  884. {
  885. if (dev->type != &i2c_adapter_type)
  886. return 0;
  887. return i2c_do_add_adapter(data, to_i2c_adapter(dev));
  888. }
  889. /*
  890. * An i2c_driver is used with one or more i2c_client (device) nodes to access
  891. * i2c slave chips, on a bus instance associated with some i2c_adapter.
  892. */
  893. int i2c_register_driver(struct module *owner, struct i2c_driver *driver)
  894. {
  895. int res;
  896. /* Can't register until after driver model init */
  897. if (unlikely(WARN_ON(!i2c_bus_type.p)))
  898. return -EAGAIN;
  899. /* add the driver to the list of i2c drivers in the driver core */
  900. driver->driver.owner = owner;
  901. driver->driver.bus = &i2c_bus_type;
  902. /* When registration returns, the driver core
  903. * will have called probe() for all matching-but-unbound devices.
  904. */
  905. res = driver_register(&driver->driver);
  906. if (res)
  907. return res;
  908. pr_debug("i2c-core: driver [%s] registered\n", driver->driver.name);
  909. INIT_LIST_HEAD(&driver->clients);
  910. /* Walk the adapters that are already present */
  911. mutex_lock(&core_lock);
  912. bus_for_each_dev(&i2c_bus_type, NULL, driver, __process_new_driver);
  913. mutex_unlock(&core_lock);
  914. return 0;
  915. }
  916. EXPORT_SYMBOL(i2c_register_driver);
  917. static int __process_removed_driver(struct device *dev, void *data)
  918. {
  919. if (dev->type != &i2c_adapter_type)
  920. return 0;
  921. return i2c_do_del_adapter(data, to_i2c_adapter(dev));
  922. }
  923. /**
  924. * i2c_del_driver - unregister I2C driver
  925. * @driver: the driver being unregistered
  926. * Context: can sleep
  927. */
  928. void i2c_del_driver(struct i2c_driver *driver)
  929. {
  930. mutex_lock(&core_lock);
  931. bus_for_each_dev(&i2c_bus_type, NULL, driver, __process_removed_driver);
  932. mutex_unlock(&core_lock);
  933. driver_unregister(&driver->driver);
  934. pr_debug("i2c-core: driver [%s] unregistered\n", driver->driver.name);
  935. }
  936. EXPORT_SYMBOL(i2c_del_driver);
  937. /* ------------------------------------------------------------------------- */
  938. /**
  939. * i2c_use_client - increments the reference count of the i2c client structure
  940. * @client: the client being referenced
  941. *
  942. * Each live reference to a client should be refcounted. The driver model does
  943. * that automatically as part of driver binding, so that most drivers don't
  944. * need to do this explicitly: they hold a reference until they're unbound
  945. * from the device.
  946. *
  947. * A pointer to the client with the incremented reference counter is returned.
  948. */
  949. struct i2c_client *i2c_use_client(struct i2c_client *client)
  950. {
  951. if (client && get_device(&client->dev))
  952. return client;
  953. return NULL;
  954. }
  955. EXPORT_SYMBOL(i2c_use_client);
  956. /**
  957. * i2c_release_client - release a use of the i2c client structure
  958. * @client: the client being no longer referenced
  959. *
  960. * Must be called when a user of a client is finished with it.
  961. */
  962. void i2c_release_client(struct i2c_client *client)
  963. {
  964. if (client)
  965. put_device(&client->dev);
  966. }
  967. EXPORT_SYMBOL(i2c_release_client);
  968. struct i2c_cmd_arg {
  969. unsigned cmd;
  970. void *arg;
  971. };
  972. static int i2c_cmd(struct device *dev, void *_arg)
  973. {
  974. struct i2c_client *client = i2c_verify_client(dev);
  975. struct i2c_cmd_arg *arg = _arg;
  976. if (client && client->driver && client->driver->command)
  977. client->driver->command(client, arg->cmd, arg->arg);
  978. return 0;
  979. }
  980. void i2c_clients_command(struct i2c_adapter *adap, unsigned int cmd, void *arg)
  981. {
  982. struct i2c_cmd_arg cmd_arg;
  983. cmd_arg.cmd = cmd;
  984. cmd_arg.arg = arg;
  985. device_for_each_child(&adap->dev, &cmd_arg, i2c_cmd);
  986. }
  987. EXPORT_SYMBOL(i2c_clients_command);
  988. static int __init i2c_init(void)
  989. {
  990. int retval;
  991. retval = bus_register(&i2c_bus_type);
  992. if (retval)
  993. return retval;
  994. #ifdef CONFIG_I2C_COMPAT
  995. i2c_adapter_compat_class = class_compat_register("i2c-adapter");
  996. if (!i2c_adapter_compat_class) {
  997. retval = -ENOMEM;
  998. goto bus_err;
  999. }
  1000. #endif
  1001. retval = i2c_add_driver(&dummy_driver);
  1002. if (retval)
  1003. goto class_err;
  1004. return 0;
  1005. class_err:
  1006. #ifdef CONFIG_I2C_COMPAT
  1007. class_compat_unregister(i2c_adapter_compat_class);
  1008. bus_err:
  1009. #endif
  1010. bus_unregister(&i2c_bus_type);
  1011. return retval;
  1012. }
  1013. static void __exit i2c_exit(void)
  1014. {
  1015. i2c_del_driver(&dummy_driver);
  1016. #ifdef CONFIG_I2C_COMPAT
  1017. class_compat_unregister(i2c_adapter_compat_class);
  1018. #endif
  1019. bus_unregister(&i2c_bus_type);
  1020. }
  1021. /* We must initialize early, because some subsystems register i2c drivers
  1022. * in subsys_initcall() code, but are linked (and initialized) before i2c.
  1023. */
  1024. postcore_initcall(i2c_init);
  1025. module_exit(i2c_exit);
  1026. /* ----------------------------------------------------
  1027. * the functional interface to the i2c busses.
  1028. * ----------------------------------------------------
  1029. */
  1030. /**
  1031. * i2c_transfer - execute a single or combined I2C message
  1032. * @adap: Handle to I2C bus
  1033. * @msgs: One or more messages to execute before STOP is issued to
  1034. * terminate the operation; each message begins with a START.
  1035. * @num: Number of messages to be executed.
  1036. *
  1037. * Returns negative errno, else the number of messages executed.
  1038. *
  1039. * Note that there is no requirement that each message be sent to
  1040. * the same slave address, although that is the most common model.
  1041. */
  1042. int i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
  1043. {
  1044. unsigned long orig_jiffies;
  1045. int ret, try;
  1046. /* REVISIT the fault reporting model here is weak:
  1047. *
  1048. * - When we get an error after receiving N bytes from a slave,
  1049. * there is no way to report "N".
  1050. *
  1051. * - When we get a NAK after transmitting N bytes to a slave,
  1052. * there is no way to report "N" ... or to let the master
  1053. * continue executing the rest of this combined message, if
  1054. * that's the appropriate response.
  1055. *
  1056. * - When for example "num" is two and we successfully complete
  1057. * the first message but get an error part way through the
  1058. * second, it's unclear whether that should be reported as
  1059. * one (discarding status on the second message) or errno
  1060. * (discarding status on the first one).
  1061. */
  1062. if (adap->algo->master_xfer) {
  1063. #ifdef DEBUG
  1064. for (ret = 0; ret < num; ret++) {
  1065. dev_dbg(&adap->dev, "master_xfer[%d] %c, addr=0x%02x, "
  1066. "len=%d%s\n", ret, (msgs[ret].flags & I2C_M_RD)
  1067. ? 'R' : 'W', msgs[ret].addr, msgs[ret].len,
  1068. (msgs[ret].flags & I2C_M_RECV_LEN) ? "+" : "");
  1069. }
  1070. #endif
  1071. if (in_atomic() || irqs_disabled()) {
  1072. ret = i2c_trylock_adapter(adap);
  1073. if (!ret)
  1074. /* I2C activity is ongoing. */
  1075. return -EAGAIN;
  1076. } else {
  1077. i2c_lock_adapter(adap);
  1078. }
  1079. /* Retry automatically on arbitration loss */
  1080. orig_jiffies = jiffies;
  1081. for (ret = 0, try = 0; try <= adap->retries; try++) {
  1082. ret = adap->algo->master_xfer(adap, msgs, num);
  1083. if (ret != -EAGAIN)
  1084. break;
  1085. if (time_after(jiffies, orig_jiffies + adap->timeout))
  1086. break;
  1087. }
  1088. i2c_unlock_adapter(adap);
  1089. return ret;
  1090. } else {
  1091. dev_dbg(&adap->dev, "I2C level transfers not supported\n");
  1092. return -EOPNOTSUPP;
  1093. }
  1094. }
  1095. EXPORT_SYMBOL(i2c_transfer);
  1096. /**
  1097. * i2c_master_send - issue a single I2C message in master transmit mode
  1098. * @client: Handle to slave device
  1099. * @buf: Data that will be written to the slave
  1100. * @count: How many bytes to write, must be less than 64k since msg.len is u16
  1101. *
  1102. * Returns negative errno, or else the number of bytes written.
  1103. */
  1104. int i2c_master_send(struct i2c_client *client, const char *buf, int count)
  1105. {
  1106. int ret;
  1107. struct i2c_adapter *adap = client->adapter;
  1108. struct i2c_msg msg;
  1109. msg.addr = client->addr;
  1110. msg.flags = client->flags & I2C_M_TEN;
  1111. msg.len = count;
  1112. msg.buf = (char *)buf;
  1113. ret = i2c_transfer(adap, &msg, 1);
  1114. /* If everything went ok (i.e. 1 msg transmitted), return #bytes
  1115. transmitted, else error code. */
  1116. return (ret == 1) ? count : ret;
  1117. }
  1118. EXPORT_SYMBOL(i2c_master_send);
  1119. /**
  1120. * i2c_master_recv - issue a single I2C message in master receive mode
  1121. * @client: Handle to slave device
  1122. * @buf: Where to store data read from slave
  1123. * @count: How many bytes to read, must be less than 64k since msg.len is u16
  1124. *
  1125. * Returns negative errno, or else the number of bytes read.
  1126. */
  1127. int i2c_master_recv(struct i2c_client *client, char *buf, int count)
  1128. {
  1129. struct i2c_adapter *adap = client->adapter;
  1130. struct i2c_msg msg;
  1131. int ret;
  1132. msg.addr = client->addr;
  1133. msg.flags = client->flags & I2C_M_TEN;
  1134. msg.flags |= I2C_M_RD;
  1135. msg.len = count;
  1136. msg.buf = buf;
  1137. ret = i2c_transfer(adap, &msg, 1);
  1138. /* If everything went ok (i.e. 1 msg transmitted), return #bytes
  1139. transmitted, else error code. */
  1140. return (ret == 1) ? count : ret;
  1141. }
  1142. EXPORT_SYMBOL(i2c_master_recv);
  1143. /* ----------------------------------------------------
  1144. * the i2c address scanning function
  1145. * Will not work for 10-bit addresses!
  1146. * ----------------------------------------------------
  1147. */
  1148. /*
  1149. * Legacy default probe function, mostly relevant for SMBus. The default
  1150. * probe method is a quick write, but it is known to corrupt the 24RF08
  1151. * EEPROMs due to a state machine bug, and could also irreversibly
  1152. * write-protect some EEPROMs, so for address ranges 0x30-0x37 and 0x50-0x5f,
  1153. * we use a short byte read instead. Also, some bus drivers don't implement
  1154. * quick write, so we fallback to a byte read in that case too.
  1155. * On x86, there is another special case for FSC hardware monitoring chips,
  1156. * which want regular byte reads (address 0x73.) Fortunately, these are the
  1157. * only known chips using this I2C address on PC hardware.
  1158. * Returns 1 if probe succeeded, 0 if not.
  1159. */
  1160. static int i2c_default_probe(struct i2c_adapter *adap, unsigned short addr)
  1161. {
  1162. int err;
  1163. union i2c_smbus_data dummy;
  1164. #ifdef CONFIG_X86
  1165. if (addr == 0x73 && (adap->class & I2C_CLASS_HWMON)
  1166. && i2c_check_functionality(adap, I2C_FUNC_SMBUS_READ_BYTE_DATA))
  1167. err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0,
  1168. I2C_SMBUS_BYTE_DATA, &dummy);
  1169. else
  1170. #endif
  1171. if ((addr & ~0x07) == 0x30 || (addr & ~0x0f) == 0x50
  1172. || !i2c_check_functionality(adap, I2C_FUNC_SMBUS_QUICK))
  1173. err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0,
  1174. I2C_SMBUS_BYTE, &dummy);
  1175. else
  1176. err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_WRITE, 0,
  1177. I2C_SMBUS_QUICK, NULL);
  1178. return err >= 0;
  1179. }
  1180. static int i2c_detect_address(struct i2c_client *temp_client,
  1181. struct i2c_driver *driver)
  1182. {
  1183. struct i2c_board_info info;
  1184. struct i2c_adapter *adapter = temp_client->adapter;
  1185. int addr = temp_client->addr;
  1186. int err;
  1187. /* Make sure the address is valid */
  1188. err = i2c_check_addr_validity(addr);
  1189. if (err) {
  1190. dev_warn(&adapter->dev, "Invalid probe address 0x%02x\n",
  1191. addr);
  1192. return err;
  1193. }
  1194. /* Skip if already in use */
  1195. if (i2c_check_addr_busy(adapter, addr))
  1196. return 0;
  1197. /* Make sure there is something at this address */
  1198. if (!i2c_default_probe(adapter, addr))
  1199. return 0;
  1200. /* Finally call the custom detection function */
  1201. memset(&info, 0, sizeof(struct i2c_board_info));
  1202. info.addr = addr;
  1203. err = driver->detect(temp_client, &info);
  1204. if (err) {
  1205. /* -ENODEV is returned if the detection fails. We catch it
  1206. here as this isn't an error. */
  1207. return err == -ENODEV ? 0 : err;
  1208. }
  1209. /* Consistency check */
  1210. if (info.type[0] == '\0') {
  1211. dev_err(&adapter->dev, "%s detection function provided "
  1212. "no name for 0x%x\n", driver->driver.name,
  1213. addr);
  1214. } else {
  1215. struct i2c_client *client;
  1216. /* Detection succeeded, instantiate the device */
  1217. dev_dbg(&adapter->dev, "Creating %s at 0x%02x\n",
  1218. info.type, info.addr);
  1219. client = i2c_new_device(adapter, &info);
  1220. if (client)
  1221. list_add_tail(&client->detected, &driver->clients);
  1222. else
  1223. dev_err(&adapter->dev, "Failed creating %s at 0x%02x\n",
  1224. info.type, info.addr);
  1225. }
  1226. return 0;
  1227. }
  1228. static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver)
  1229. {
  1230. const unsigned short *address_list;
  1231. struct i2c_client *temp_client;
  1232. int i, err = 0;
  1233. int adap_id = i2c_adapter_id(adapter);
  1234. address_list = driver->address_list;
  1235. if (!driver->detect || !address_list)
  1236. return 0;
  1237. /* Set up a temporary client to help detect callback */
  1238. temp_client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
  1239. if (!temp_client)
  1240. return -ENOMEM;
  1241. temp_client->adapter = adapter;
  1242. /* Stop here if the classes do not match */
  1243. if (!(adapter->class & driver->class))
  1244. goto exit_free;
  1245. /* Stop here if the bus doesn't support probing */
  1246. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_READ_BYTE)) {
  1247. if (address_list[0] == I2C_CLIENT_END)
  1248. goto exit_free;
  1249. dev_warn(&adapter->dev, "Probing not supported\n");
  1250. err = -EOPNOTSUPP;
  1251. goto exit_free;
  1252. }
  1253. for (i = 0; address_list[i] != I2C_CLIENT_END; i += 1) {
  1254. dev_dbg(&adapter->dev, "found normal entry for adapter %d, "
  1255. "addr 0x%02x\n", adap_id, address_list[i]);
  1256. temp_client->addr = address_list[i];
  1257. err = i2c_detect_address(temp_client, driver);
  1258. if (err)
  1259. goto exit_free;
  1260. }
  1261. exit_free:
  1262. kfree(temp_client);
  1263. return err;
  1264. }
  1265. int i2c_probe_func_quick_read(struct i2c_adapter *adap, unsigned short addr)
  1266. {
  1267. return i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0,
  1268. I2C_SMBUS_QUICK, NULL) >= 0;
  1269. }
  1270. EXPORT_SYMBOL_GPL(i2c_probe_func_quick_read);
  1271. struct i2c_client *
  1272. i2c_new_probed_device(struct i2c_adapter *adap,
  1273. struct i2c_board_info *info,
  1274. unsigned short const *addr_list,
  1275. int (*probe)(struct i2c_adapter *, unsigned short addr))
  1276. {
  1277. int i;
  1278. if (!probe) {
  1279. /* Stop here if the bus doesn't support probing */
  1280. if (!i2c_check_functionality(adap, I2C_FUNC_SMBUS_READ_BYTE)) {
  1281. dev_err(&adap->dev, "Probing not supported\n");
  1282. return NULL;
  1283. }
  1284. probe = i2c_default_probe;
  1285. }
  1286. for (i = 0; addr_list[i] != I2C_CLIENT_END; i++) {
  1287. /* Check address validity */
  1288. if (i2c_check_addr_validity(addr_list[i]) < 0) {
  1289. dev_warn(&adap->dev, "Invalid 7-bit address "
  1290. "0x%02x\n", addr_list[i]);
  1291. continue;
  1292. }
  1293. /* Check address availability */
  1294. if (i2c_check_addr_busy(adap, addr_list[i])) {
  1295. dev_dbg(&adap->dev, "Address 0x%02x already in "
  1296. "use, not probing\n", addr_list[i]);
  1297. continue;
  1298. }
  1299. /* Test address responsiveness */
  1300. if (probe(adap, addr_list[i]))
  1301. break;
  1302. }
  1303. if (addr_list[i] == I2C_CLIENT_END) {
  1304. dev_dbg(&adap->dev, "Probing failed, no device found\n");
  1305. return NULL;
  1306. }
  1307. info->addr = addr_list[i];
  1308. return i2c_new_device(adap, info);
  1309. }
  1310. EXPORT_SYMBOL_GPL(i2c_new_probed_device);
  1311. struct i2c_adapter *i2c_get_adapter(int id)
  1312. {
  1313. struct i2c_adapter *adapter;
  1314. mutex_lock(&core_lock);
  1315. adapter = idr_find(&i2c_adapter_idr, id);
  1316. if (adapter && !try_module_get(adapter->owner))
  1317. adapter = NULL;
  1318. mutex_unlock(&core_lock);
  1319. return adapter;
  1320. }
  1321. EXPORT_SYMBOL(i2c_get_adapter);
  1322. void i2c_put_adapter(struct i2c_adapter *adap)
  1323. {
  1324. module_put(adap->owner);
  1325. }
  1326. EXPORT_SYMBOL(i2c_put_adapter);
  1327. /* The SMBus parts */
  1328. #define POLY (0x1070U << 3)
  1329. static u8 crc8(u16 data)
  1330. {
  1331. int i;
  1332. for (i = 0; i < 8; i++) {
  1333. if (data & 0x8000)
  1334. data = data ^ POLY;
  1335. data = data << 1;
  1336. }
  1337. return (u8)(data >> 8);
  1338. }
  1339. /* Incremental CRC8 over count bytes in the array pointed to by p */
  1340. static u8 i2c_smbus_pec(u8 crc, u8 *p, size_t count)
  1341. {
  1342. int i;
  1343. for (i = 0; i < count; i++)
  1344. crc = crc8((crc ^ p[i]) << 8);
  1345. return crc;
  1346. }
  1347. /* Assume a 7-bit address, which is reasonable for SMBus */
  1348. static u8 i2c_smbus_msg_pec(u8 pec, struct i2c_msg *msg)
  1349. {
  1350. /* The address will be sent first */
  1351. u8 addr = (msg->addr << 1) | !!(msg->flags & I2C_M_RD);
  1352. pec = i2c_smbus_pec(pec, &addr, 1);
  1353. /* The data buffer follows */
  1354. return i2c_smbus_pec(pec, msg->buf, msg->len);
  1355. }
  1356. /* Used for write only transactions */
  1357. static inline void i2c_smbus_add_pec(struct i2c_msg *msg)
  1358. {
  1359. msg->buf[msg->len] = i2c_smbus_msg_pec(0, msg);
  1360. msg->len++;
  1361. }
  1362. /* Return <0 on CRC error
  1363. If there was a write before this read (most cases) we need to take the
  1364. partial CRC from the write part into account.
  1365. Note that this function does modify the message (we need to decrease the
  1366. message length to hide the CRC byte from the caller). */
  1367. static int i2c_smbus_check_pec(u8 cpec, struct i2c_msg *msg)
  1368. {
  1369. u8 rpec = msg->buf[--msg->len];
  1370. cpec = i2c_smbus_msg_pec(cpec, msg);
  1371. if (rpec != cpec) {
  1372. pr_debug("i2c-core: Bad PEC 0x%02x vs. 0x%02x\n",
  1373. rpec, cpec);
  1374. return -EBADMSG;
  1375. }
  1376. return 0;
  1377. }
  1378. /**
  1379. * i2c_smbus_read_byte - SMBus "receive byte" protocol
  1380. * @client: Handle to slave device
  1381. *
  1382. * This executes the SMBus "receive byte" protocol, returning negative errno
  1383. * else the byte received from the device.
  1384. */
  1385. s32 i2c_smbus_read_byte(struct i2c_client *client)
  1386. {
  1387. union i2c_smbus_data data;
  1388. int status;
  1389. status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
  1390. I2C_SMBUS_READ, 0,
  1391. I2C_SMBUS_BYTE, &data);
  1392. return (status < 0) ? status : data.byte;
  1393. }
  1394. EXPORT_SYMBOL(i2c_smbus_read_byte);
  1395. /**
  1396. * i2c_smbus_write_byte - SMBus "send byte" protocol
  1397. * @client: Handle to slave device
  1398. * @value: Byte to be sent
  1399. *
  1400. * This executes the SMBus "send byte" protocol, returning negative errno
  1401. * else zero on success.
  1402. */
  1403. s32 i2c_smbus_write_byte(struct i2c_client *client, u8 value)
  1404. {
  1405. return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
  1406. I2C_SMBUS_WRITE, value, I2C_SMBUS_BYTE, NULL);
  1407. }
  1408. EXPORT_SYMBOL(i2c_smbus_write_byte);
  1409. /**
  1410. * i2c_smbus_read_byte_data - SMBus "read byte" protocol
  1411. * @client: Handle to slave device
  1412. * @command: Byte interpreted by slave
  1413. *
  1414. * This executes the SMBus "read byte" protocol, returning negative errno
  1415. * else a data byte received from the device.
  1416. */
  1417. s32 i2c_smbus_read_byte_data(struct i2c_client *client, u8 command)
  1418. {
  1419. union i2c_smbus_data data;
  1420. int status;
  1421. status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
  1422. I2C_SMBUS_READ, command,
  1423. I2C_SMBUS_BYTE_DATA, &data);
  1424. return (status < 0) ? status : data.byte;
  1425. }
  1426. EXPORT_SYMBOL(i2c_smbus_read_byte_data);
  1427. /**
  1428. * i2c_smbus_write_byte_data - SMBus "write byte" protocol
  1429. * @client: Handle to slave device
  1430. * @command: Byte interpreted by slave
  1431. * @value: Byte being written
  1432. *
  1433. * This executes the SMBus "write byte" protocol, returning negative errno
  1434. * else zero on success.
  1435. */
  1436. s32 i2c_smbus_write_byte_data(struct i2c_client *client, u8 command, u8 value)
  1437. {
  1438. union i2c_smbus_data data;
  1439. data.byte = value;
  1440. return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
  1441. I2C_SMBUS_WRITE, command,
  1442. I2C_SMBUS_BYTE_DATA, &data);
  1443. }
  1444. EXPORT_SYMBOL(i2c_smbus_write_byte_data);
  1445. /**
  1446. * i2c_smbus_read_word_data - SMBus "read word" protocol
  1447. * @client: Handle to slave device
  1448. * @command: Byte interpreted by slave
  1449. *
  1450. * This executes the SMBus "read word" protocol, returning negative errno
  1451. * else a 16-bit unsigned "word" received from the device.
  1452. */
  1453. s32 i2c_smbus_read_word_data(struct i2c_client *client, u8 command)
  1454. {
  1455. union i2c_smbus_data data;
  1456. int status;
  1457. status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
  1458. I2C_SMBUS_READ, command,
  1459. I2C_SMBUS_WORD_DATA, &data);
  1460. return (status < 0) ? status : data.word;
  1461. }
  1462. EXPORT_SYMBOL(i2c_smbus_read_word_data);
  1463. /**
  1464. * i2c_smbus_write_word_data - SMBus "write word" protocol
  1465. * @client: Handle to slave device
  1466. * @command: Byte interpreted by slave
  1467. * @value: 16-bit "word" being written
  1468. *
  1469. * This executes the SMBus "write word" protocol, returning negative errno
  1470. * else zero on success.
  1471. */
  1472. s32 i2c_smbus_write_word_data(struct i2c_client *client, u8 command, u16 value)
  1473. {
  1474. union i2c_smbus_data data;
  1475. data.word = value;
  1476. return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
  1477. I2C_SMBUS_WRITE, command,
  1478. I2C_SMBUS_WORD_DATA, &data);
  1479. }
  1480. EXPORT_SYMBOL(i2c_smbus_write_word_data);
  1481. /**
  1482. * i2c_smbus_process_call - SMBus "process call" protocol
  1483. * @client: Handle to slave device
  1484. * @command: Byte interpreted by slave
  1485. * @value: 16-bit "word" being written
  1486. *
  1487. * This executes the SMBus "process call" protocol, returning negative errno
  1488. * else a 16-bit unsigned "word" received from the device.
  1489. */
  1490. s32 i2c_smbus_process_call(struct i2c_client *client, u8 command, u16 value)
  1491. {
  1492. union i2c_smbus_data data;
  1493. int status;
  1494. data.word = value;
  1495. status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
  1496. I2C_SMBUS_WRITE, command,
  1497. I2C_SMBUS_PROC_CALL, &data);
  1498. return (status < 0) ? status : data.word;
  1499. }
  1500. EXPORT_SYMBOL(i2c_smbus_process_call);
  1501. /**
  1502. * i2c_smbus_read_block_data - SMBus "block read" protocol
  1503. * @client: Handle to slave device
  1504. * @command: Byte interpreted by slave
  1505. * @values: Byte array into which data will be read; big enough to hold
  1506. * the data returned by the slave. SMBus allows at most 32 bytes.
  1507. *
  1508. * This executes the SMBus "block read" protocol, returning negative errno
  1509. * else the number of data bytes in the slave's response.
  1510. *
  1511. * Note that using this function requires that the client's adapter support
  1512. * the I2C_FUNC_SMBUS_READ_BLOCK_DATA functionality. Not all adapter drivers
  1513. * support this; its emulation through I2C messaging relies on a specific
  1514. * mechanism (I2C_M_RECV_LEN) which may not be implemented.
  1515. */
  1516. s32 i2c_smbus_read_block_data(struct i2c_client *client, u8 command,
  1517. u8 *values)
  1518. {
  1519. union i2c_smbus_data data;
  1520. int status;
  1521. status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
  1522. I2C_SMBUS_READ, command,
  1523. I2C_SMBUS_BLOCK_DATA, &data);
  1524. if (status)
  1525. return status;
  1526. memcpy(values, &data.block[1], data.block[0]);
  1527. return data.block[0];
  1528. }
  1529. EXPORT_SYMBOL(i2c_smbus_read_block_data);
  1530. /**
  1531. * i2c_smbus_write_block_data - SMBus "block write" protocol
  1532. * @client: Handle to slave device
  1533. * @command: Byte interpreted by slave
  1534. * @length: Size of data block; SMBus allows at most 32 bytes
  1535. * @values: Byte array which will be written.
  1536. *
  1537. * This executes the SMBus "block write" protocol, returning negative errno
  1538. * else zero on success.
  1539. */
  1540. s32 i2c_smbus_write_block_data(struct i2c_client *client, u8 command,
  1541. u8 length, const u8 *values)
  1542. {
  1543. union i2c_smbus_data data;
  1544. if (length > I2C_SMBUS_BLOCK_MAX)
  1545. length = I2C_SMBUS_BLOCK_MAX;
  1546. data.block[0] = length;
  1547. memcpy(&data.block[1], values, length);
  1548. return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
  1549. I2C_SMBUS_WRITE, command,
  1550. I2C_SMBUS_BLOCK_DATA, &data);
  1551. }
  1552. EXPORT_SYMBOL(i2c_smbus_write_block_data);
  1553. /* Returns the number of read bytes */
  1554. s32 i2c_smbus_read_i2c_block_data(struct i2c_client *client, u8 command,
  1555. u8 length, u8 *values)
  1556. {
  1557. union i2c_smbus_data data;
  1558. int status;
  1559. if (length > I2C_SMBUS_BLOCK_MAX)
  1560. length = I2C_SMBUS_BLOCK_MAX;
  1561. data.block[0] = length;
  1562. status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
  1563. I2C_SMBUS_READ, command,
  1564. I2C_SMBUS_I2C_BLOCK_DATA, &data);
  1565. if (status < 0)
  1566. return status;
  1567. memcpy(values, &data.block[1], data.block[0]);
  1568. return data.block[0];
  1569. }
  1570. EXPORT_SYMBOL(i2c_smbus_read_i2c_block_data);
  1571. s32 i2c_smbus_write_i2c_block_data(struct i2c_client *client, u8 command,
  1572. u8 length, const u8 *values)
  1573. {
  1574. union i2c_smbus_data data;
  1575. if (length > I2C_SMBUS_BLOCK_MAX)
  1576. length = I2C_SMBUS_BLOCK_MAX;
  1577. data.block[0] = length;
  1578. memcpy(data.block + 1, values, length);
  1579. return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
  1580. I2C_SMBUS_WRITE, command,
  1581. I2C_SMBUS_I2C_BLOCK_DATA, &data);
  1582. }
  1583. EXPORT_SYMBOL(i2c_smbus_write_i2c_block_data);
  1584. /* Simulate a SMBus command using the i2c protocol
  1585. No checking of parameters is done! */
  1586. static s32 i2c_smbus_xfer_emulated(struct i2c_adapter *adapter, u16 addr,
  1587. unsigned short flags,
  1588. char read_write, u8 command, int size,
  1589. union i2c_smbus_data *data)
  1590. {
  1591. /* So we need to generate a series of msgs. In the case of writing, we
  1592. need to use only one message; when reading, we need two. We initialize
  1593. most things with sane defaults, to keep the code below somewhat
  1594. simpler. */
  1595. unsigned char msgbuf0[I2C_SMBUS_BLOCK_MAX+3];
  1596. unsigned char msgbuf1[I2C_SMBUS_BLOCK_MAX+2];
  1597. int num = read_write == I2C_SMBUS_READ ? 2 : 1;
  1598. struct i2c_msg msg[2] = { { addr, flags, 1, msgbuf0 },
  1599. { addr, flags | I2C_M_RD, 0, msgbuf1 }
  1600. };
  1601. int i;
  1602. u8 partial_pec = 0;
  1603. int status;
  1604. msgbuf0[0] = command;
  1605. switch (size) {
  1606. case I2C_SMBUS_QUICK:
  1607. msg[0].len = 0;
  1608. /* Special case: The read/write field is used as data */
  1609. msg[0].flags = flags | (read_write == I2C_SMBUS_READ ?
  1610. I2C_M_RD : 0);
  1611. num = 1;
  1612. break;
  1613. case I2C_SMBUS_BYTE:
  1614. if (read_write == I2C_SMBUS_READ) {
  1615. /* Special case: only a read! */
  1616. msg[0].flags = I2C_M_RD | flags;
  1617. num = 1;
  1618. }
  1619. break;
  1620. case I2C_SMBUS_BYTE_DATA:
  1621. if (read_write == I2C_SMBUS_READ)
  1622. msg[1].len = 1;
  1623. else {
  1624. msg[0].len = 2;
  1625. msgbuf0[1] = data->byte;
  1626. }
  1627. break;
  1628. case I2C_SMBUS_WORD_DATA:
  1629. if (read_write == I2C_SMBUS_READ)
  1630. msg[1].len = 2;
  1631. else {
  1632. msg[0].len = 3;
  1633. msgbuf0[1] = data->word & 0xff;
  1634. msgbuf0[2] = data->word >> 8;
  1635. }
  1636. break;
  1637. case I2C_SMBUS_PROC_CALL:
  1638. num = 2; /* Special case */
  1639. read_write = I2C_SMBUS_READ;
  1640. msg[0].len = 3;
  1641. msg[1].len = 2;
  1642. msgbuf0[1] = data->word & 0xff;
  1643. msgbuf0[2] = data->word >> 8;
  1644. break;
  1645. case I2C_SMBUS_BLOCK_DATA:
  1646. if (read_write == I2C_SMBUS_READ) {
  1647. msg[1].flags |= I2C_M_RECV_LEN;
  1648. msg[1].len = 1; /* block length will be added by
  1649. the underlying bus driver */
  1650. } else {
  1651. msg[0].len = data->block[0] + 2;
  1652. if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 2) {
  1653. dev_err(&adapter->dev,
  1654. "Invalid block write size %d\n",
  1655. data->block[0]);
  1656. return -EINVAL;
  1657. }
  1658. for (i = 1; i < msg[0].len; i++)
  1659. msgbuf0[i] = data->block[i-1];
  1660. }
  1661. break;
  1662. case I2C_SMBUS_BLOCK_PROC_CALL:
  1663. num = 2; /* Another special case */
  1664. read_write = I2C_SMBUS_READ;
  1665. if (data->block[0] > I2C_SMBUS_BLOCK_MAX) {
  1666. dev_err(&adapter->dev,
  1667. "Invalid block write size %d\n",
  1668. data->block[0]);
  1669. return -EINVAL;
  1670. }
  1671. msg[0].len = data->block[0] + 2;
  1672. for (i = 1; i < msg[0].len; i++)
  1673. msgbuf0[i] = data->block[i-1];
  1674. msg[1].flags |= I2C_M_RECV_LEN;
  1675. msg[1].len = 1; /* block length will be added by
  1676. the underlying bus driver */
  1677. break;
  1678. case I2C_SMBUS_I2C_BLOCK_DATA:
  1679. if (read_write == I2C_SMBUS_READ) {
  1680. msg[1].len = data->block[0];
  1681. } else {
  1682. msg[0].len = data->block[0] + 1;
  1683. if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 1) {
  1684. dev_err(&adapter->dev,
  1685. "Invalid block write size %d\n",
  1686. data->block[0]);
  1687. return -EINVAL;
  1688. }
  1689. for (i = 1; i <= data->block[0]; i++)
  1690. msgbuf0[i] = data->block[i];
  1691. }
  1692. break;
  1693. default:
  1694. dev_err(&adapter->dev, "Unsupported transaction %d\n", size);
  1695. return -EOPNOTSUPP;
  1696. }
  1697. i = ((flags & I2C_CLIENT_PEC) && size != I2C_SMBUS_QUICK
  1698. && size != I2C_SMBUS_I2C_BLOCK_DATA);
  1699. if (i) {
  1700. /* Compute PEC if first message is a write */
  1701. if (!(msg[0].flags & I2C_M_RD)) {
  1702. if (num == 1) /* Write only */
  1703. i2c_smbus_add_pec(&msg[0]);
  1704. else /* Write followed by read */
  1705. partial_pec = i2c_smbus_msg_pec(0, &msg[0]);
  1706. }
  1707. /* Ask for PEC if last message is a read */
  1708. if (msg[num-1].flags & I2C_M_RD)
  1709. msg[num-1].len++;
  1710. }
  1711. status = i2c_transfer(adapter, msg, num);
  1712. if (status < 0)
  1713. return status;
  1714. /* Check PEC if last message is a read */
  1715. if (i && (msg[num-1].flags & I2C_M_RD)) {
  1716. status = i2c_smbus_check_pec(partial_pec, &msg[num-1]);
  1717. if (status < 0)
  1718. return status;
  1719. }
  1720. if (read_write == I2C_SMBUS_READ)
  1721. switch (size) {
  1722. case I2C_SMBUS_BYTE:
  1723. data->byte = msgbuf0[0];
  1724. break;
  1725. case I2C_SMBUS_BYTE_DATA:
  1726. data->byte = msgbuf1[0];
  1727. break;
  1728. case I2C_SMBUS_WORD_DATA:
  1729. case I2C_SMBUS_PROC_CALL:
  1730. data->word = msgbuf1[0] | (msgbuf1[1] << 8);
  1731. break;
  1732. case I2C_SMBUS_I2C_BLOCK_DATA:
  1733. for (i = 0; i < data->block[0]; i++)
  1734. data->block[i+1] = msgbuf1[i];
  1735. break;
  1736. case I2C_SMBUS_BLOCK_DATA:
  1737. case I2C_SMBUS_BLOCK_PROC_CALL:
  1738. for (i = 0; i < msgbuf1[0] + 1; i++)
  1739. data->block[i] = msgbuf1[i];
  1740. break;
  1741. }
  1742. return 0;
  1743. }
  1744. /**
  1745. * i2c_smbus_xfer - execute SMBus protocol operations
  1746. * @adapter: Handle to I2C bus
  1747. * @addr: Address of SMBus slave on that bus
  1748. * @flags: I2C_CLIENT_* flags (usually zero or I2C_CLIENT_PEC)
  1749. * @read_write: I2C_SMBUS_READ or I2C_SMBUS_WRITE
  1750. * @command: Byte interpreted by slave, for protocols which use such bytes
  1751. * @protocol: SMBus protocol operation to execute, such as I2C_SMBUS_PROC_CALL
  1752. * @data: Data to be read or written
  1753. *
  1754. * This executes an SMBus protocol operation, and returns a negative
  1755. * errno code else zero on success.
  1756. */
  1757. s32 i2c_smbus_xfer(struct i2c_adapter *adapter, u16 addr, unsigned short flags,
  1758. char read_write, u8 command, int protocol,
  1759. union i2c_smbus_data *data)
  1760. {
  1761. unsigned long orig_jiffies;
  1762. int try;
  1763. s32 res;
  1764. flags &= I2C_M_TEN | I2C_CLIENT_PEC;
  1765. if (adapter->algo->smbus_xfer) {
  1766. i2c_lock_adapter(adapter);
  1767. /* Retry automatically on arbitration loss */
  1768. orig_jiffies = jiffies;
  1769. for (res = 0, try = 0; try <= adapter->retries; try++) {
  1770. res = adapter->algo->smbus_xfer(adapter, addr, flags,
  1771. read_write, command,
  1772. protocol, data);
  1773. if (res != -EAGAIN)
  1774. break;
  1775. if (time_after(jiffies,
  1776. orig_jiffies + adapter->timeout))
  1777. break;
  1778. }
  1779. i2c_unlock_adapter(adapter);
  1780. } else
  1781. res = i2c_smbus_xfer_emulated(adapter, addr, flags, read_write,
  1782. command, protocol, data);
  1783. return res;
  1784. }
  1785. EXPORT_SYMBOL(i2c_smbus_xfer);
  1786. MODULE_AUTHOR("Simon G. Vogl <simon@tk.uni-linz.ac.at>");
  1787. MODULE_DESCRIPTION("I2C-Bus main module");
  1788. MODULE_LICENSE("GPL");