i2c-core.c 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749
  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/platform_device.h>
  28. #include <linux/mutex.h>
  29. #include <linux/completion.h>
  30. #include <linux/hardirq.h>
  31. #include <linux/irqflags.h>
  32. #include <asm/uaccess.h>
  33. #include "i2c-core.h"
  34. static DEFINE_MUTEX(core_lock);
  35. static DEFINE_IDR(i2c_adapter_idr);
  36. #define is_newstyle_driver(d) ((d)->probe || (d)->remove)
  37. /* ------------------------------------------------------------------------- */
  38. static const struct i2c_device_id *i2c_match_id(const struct i2c_device_id *id,
  39. const struct i2c_client *client)
  40. {
  41. while (id->name[0]) {
  42. if (strcmp(client->name, id->name) == 0)
  43. return id;
  44. id++;
  45. }
  46. return NULL;
  47. }
  48. static int i2c_device_match(struct device *dev, struct device_driver *drv)
  49. {
  50. struct i2c_client *client = to_i2c_client(dev);
  51. struct i2c_driver *driver = to_i2c_driver(drv);
  52. /* make legacy i2c drivers bypass driver model probing entirely;
  53. * such drivers scan each i2c adapter/bus themselves.
  54. */
  55. if (!is_newstyle_driver(driver))
  56. return 0;
  57. /* match on an id table if there is one */
  58. if (driver->id_table)
  59. return i2c_match_id(driver->id_table, client) != NULL;
  60. return 0;
  61. }
  62. #ifdef CONFIG_HOTPLUG
  63. /* uevent helps with hotplug: modprobe -q $(MODALIAS) */
  64. static int i2c_device_uevent(struct device *dev, struct kobj_uevent_env *env)
  65. {
  66. struct i2c_client *client = to_i2c_client(dev);
  67. /* by definition, legacy drivers can't hotplug */
  68. if (dev->driver)
  69. return 0;
  70. if (add_uevent_var(env, "MODALIAS=%s%s",
  71. I2C_MODULE_PREFIX, client->name))
  72. return -ENOMEM;
  73. dev_dbg(dev, "uevent\n");
  74. return 0;
  75. }
  76. #else
  77. #define i2c_device_uevent NULL
  78. #endif /* CONFIG_HOTPLUG */
  79. static int i2c_device_probe(struct device *dev)
  80. {
  81. struct i2c_client *client = to_i2c_client(dev);
  82. struct i2c_driver *driver = to_i2c_driver(dev->driver);
  83. int status;
  84. if (!driver->probe || !driver->id_table)
  85. return -ENODEV;
  86. client->driver = driver;
  87. dev_dbg(dev, "probe\n");
  88. status = driver->probe(client, i2c_match_id(driver->id_table, client));
  89. if (status)
  90. client->driver = NULL;
  91. return status;
  92. }
  93. static int i2c_device_remove(struct device *dev)
  94. {
  95. struct i2c_client *client = to_i2c_client(dev);
  96. struct i2c_driver *driver;
  97. int status;
  98. if (!dev->driver)
  99. return 0;
  100. driver = to_i2c_driver(dev->driver);
  101. if (driver->remove) {
  102. dev_dbg(dev, "remove\n");
  103. status = driver->remove(client);
  104. } else {
  105. dev->driver = NULL;
  106. status = 0;
  107. }
  108. if (status == 0)
  109. client->driver = NULL;
  110. return status;
  111. }
  112. static void i2c_device_shutdown(struct device *dev)
  113. {
  114. struct i2c_driver *driver;
  115. if (!dev->driver)
  116. return;
  117. driver = to_i2c_driver(dev->driver);
  118. if (driver->shutdown)
  119. driver->shutdown(to_i2c_client(dev));
  120. }
  121. static int i2c_device_suspend(struct device * dev, pm_message_t mesg)
  122. {
  123. struct i2c_driver *driver;
  124. if (!dev->driver)
  125. return 0;
  126. driver = to_i2c_driver(dev->driver);
  127. if (!driver->suspend)
  128. return 0;
  129. return driver->suspend(to_i2c_client(dev), mesg);
  130. }
  131. static int i2c_device_resume(struct device * dev)
  132. {
  133. struct i2c_driver *driver;
  134. if (!dev->driver)
  135. return 0;
  136. driver = to_i2c_driver(dev->driver);
  137. if (!driver->resume)
  138. return 0;
  139. return driver->resume(to_i2c_client(dev));
  140. }
  141. static void i2c_client_release(struct device *dev)
  142. {
  143. struct i2c_client *client = to_i2c_client(dev);
  144. complete(&client->released);
  145. }
  146. static void i2c_client_dev_release(struct device *dev)
  147. {
  148. kfree(to_i2c_client(dev));
  149. }
  150. static ssize_t show_client_name(struct device *dev, struct device_attribute *attr, char *buf)
  151. {
  152. struct i2c_client *client = to_i2c_client(dev);
  153. return sprintf(buf, "%s\n", client->name);
  154. }
  155. static ssize_t show_modalias(struct device *dev, struct device_attribute *attr, char *buf)
  156. {
  157. struct i2c_client *client = to_i2c_client(dev);
  158. return sprintf(buf, "%s%s\n", I2C_MODULE_PREFIX, client->name);
  159. }
  160. static struct device_attribute i2c_dev_attrs[] = {
  161. __ATTR(name, S_IRUGO, show_client_name, NULL),
  162. /* modalias helps coldplug: modprobe $(cat .../modalias) */
  163. __ATTR(modalias, S_IRUGO, show_modalias, NULL),
  164. { },
  165. };
  166. struct bus_type i2c_bus_type = {
  167. .name = "i2c",
  168. .dev_attrs = i2c_dev_attrs,
  169. .match = i2c_device_match,
  170. .uevent = i2c_device_uevent,
  171. .probe = i2c_device_probe,
  172. .remove = i2c_device_remove,
  173. .shutdown = i2c_device_shutdown,
  174. .suspend = i2c_device_suspend,
  175. .resume = i2c_device_resume,
  176. };
  177. EXPORT_SYMBOL_GPL(i2c_bus_type);
  178. /**
  179. * i2c_verify_client - return parameter as i2c_client, or NULL
  180. * @dev: device, probably from some driver model iterator
  181. *
  182. * When traversing the driver model tree, perhaps using driver model
  183. * iterators like @device_for_each_child(), you can't assume very much
  184. * about the nodes you find. Use this function to avoid oopses caused
  185. * by wrongly treating some non-I2C device as an i2c_client.
  186. */
  187. struct i2c_client *i2c_verify_client(struct device *dev)
  188. {
  189. return (dev->bus == &i2c_bus_type)
  190. ? to_i2c_client(dev)
  191. : NULL;
  192. }
  193. EXPORT_SYMBOL(i2c_verify_client);
  194. /**
  195. * i2c_new_device - instantiate an i2c device for use with a new style driver
  196. * @adap: the adapter managing the device
  197. * @info: describes one I2C device; bus_num is ignored
  198. * Context: can sleep
  199. *
  200. * Create a device to work with a new style i2c driver, where binding is
  201. * handled through driver model probe()/remove() methods. This call is not
  202. * appropriate for use by mainboad initialization logic, which usually runs
  203. * during an arch_initcall() long before any i2c_adapter could exist.
  204. *
  205. * This returns the new i2c client, which may be saved for later use with
  206. * i2c_unregister_device(); or NULL to indicate an error.
  207. */
  208. struct i2c_client *
  209. i2c_new_device(struct i2c_adapter *adap, struct i2c_board_info const *info)
  210. {
  211. struct i2c_client *client;
  212. int status;
  213. client = kzalloc(sizeof *client, GFP_KERNEL);
  214. if (!client)
  215. return NULL;
  216. client->adapter = adap;
  217. client->dev.platform_data = info->platform_data;
  218. device_init_wakeup(&client->dev, info->flags & I2C_CLIENT_WAKE);
  219. client->flags = info->flags & ~I2C_CLIENT_WAKE;
  220. client->addr = info->addr;
  221. client->irq = info->irq;
  222. strlcpy(client->name, info->type, sizeof(client->name));
  223. /* a new style driver may be bound to this device when we
  224. * return from this function, or any later moment (e.g. maybe
  225. * hotplugging will load the driver module). and the device
  226. * refcount model is the standard driver model one.
  227. */
  228. status = i2c_attach_client(client);
  229. if (status < 0) {
  230. kfree(client);
  231. client = NULL;
  232. }
  233. return client;
  234. }
  235. EXPORT_SYMBOL_GPL(i2c_new_device);
  236. /**
  237. * i2c_unregister_device - reverse effect of i2c_new_device()
  238. * @client: value returned from i2c_new_device()
  239. * Context: can sleep
  240. */
  241. void i2c_unregister_device(struct i2c_client *client)
  242. {
  243. struct i2c_adapter *adapter = client->adapter;
  244. struct i2c_driver *driver = client->driver;
  245. if (driver && !is_newstyle_driver(driver)) {
  246. dev_err(&client->dev, "can't unregister devices "
  247. "with legacy drivers\n");
  248. WARN_ON(1);
  249. return;
  250. }
  251. mutex_lock(&adapter->clist_lock);
  252. list_del(&client->list);
  253. mutex_unlock(&adapter->clist_lock);
  254. device_unregister(&client->dev);
  255. }
  256. EXPORT_SYMBOL_GPL(i2c_unregister_device);
  257. static const struct i2c_device_id dummy_id[] = {
  258. { "dummy", 0 },
  259. { },
  260. };
  261. static int dummy_probe(struct i2c_client *client,
  262. const struct i2c_device_id *id)
  263. {
  264. return 0;
  265. }
  266. static int dummy_remove(struct i2c_client *client)
  267. {
  268. return 0;
  269. }
  270. static struct i2c_driver dummy_driver = {
  271. .driver.name = "dummy",
  272. .probe = dummy_probe,
  273. .remove = dummy_remove,
  274. .id_table = dummy_id,
  275. };
  276. /**
  277. * i2c_new_dummy - return a new i2c device bound to a dummy driver
  278. * @adapter: the adapter managing the device
  279. * @address: seven bit address to be used
  280. * Context: can sleep
  281. *
  282. * This returns an I2C client bound to the "dummy" driver, intended for use
  283. * with devices that consume multiple addresses. Examples of such chips
  284. * include various EEPROMS (like 24c04 and 24c08 models).
  285. *
  286. * These dummy devices have two main uses. First, most I2C and SMBus calls
  287. * except i2c_transfer() need a client handle; the dummy will be that handle.
  288. * And second, this prevents the specified address from being bound to a
  289. * different driver.
  290. *
  291. * This returns the new i2c client, which should be saved for later use with
  292. * i2c_unregister_device(); or NULL to indicate an error.
  293. */
  294. struct i2c_client *
  295. i2c_new_dummy(struct i2c_adapter *adapter, u16 address)
  296. {
  297. struct i2c_board_info info = {
  298. I2C_BOARD_INFO("dummy", address),
  299. };
  300. return i2c_new_device(adapter, &info);
  301. }
  302. EXPORT_SYMBOL_GPL(i2c_new_dummy);
  303. /* ------------------------------------------------------------------------- */
  304. /* I2C bus adapters -- one roots each I2C or SMBUS segment */
  305. static void i2c_adapter_dev_release(struct device *dev)
  306. {
  307. struct i2c_adapter *adap = to_i2c_adapter(dev);
  308. complete(&adap->dev_released);
  309. }
  310. static ssize_t
  311. show_adapter_name(struct device *dev, struct device_attribute *attr, char *buf)
  312. {
  313. struct i2c_adapter *adap = to_i2c_adapter(dev);
  314. return sprintf(buf, "%s\n", adap->name);
  315. }
  316. static struct device_attribute i2c_adapter_attrs[] = {
  317. __ATTR(name, S_IRUGO, show_adapter_name, NULL),
  318. { },
  319. };
  320. static struct class i2c_adapter_class = {
  321. .owner = THIS_MODULE,
  322. .name = "i2c-adapter",
  323. .dev_attrs = i2c_adapter_attrs,
  324. };
  325. static void i2c_scan_static_board_info(struct i2c_adapter *adapter)
  326. {
  327. struct i2c_devinfo *devinfo;
  328. mutex_lock(&__i2c_board_lock);
  329. list_for_each_entry(devinfo, &__i2c_board_list, list) {
  330. if (devinfo->busnum == adapter->nr
  331. && !i2c_new_device(adapter,
  332. &devinfo->board_info))
  333. printk(KERN_ERR "i2c-core: can't create i2c%d-%04x\n",
  334. i2c_adapter_id(adapter),
  335. devinfo->board_info.addr);
  336. }
  337. mutex_unlock(&__i2c_board_lock);
  338. }
  339. static int i2c_do_add_adapter(struct device_driver *d, void *data)
  340. {
  341. struct i2c_driver *driver = to_i2c_driver(d);
  342. struct i2c_adapter *adap = data;
  343. if (driver->attach_adapter) {
  344. /* We ignore the return code; if it fails, too bad */
  345. driver->attach_adapter(adap);
  346. }
  347. return 0;
  348. }
  349. static int i2c_register_adapter(struct i2c_adapter *adap)
  350. {
  351. int res = 0, dummy;
  352. mutex_init(&adap->bus_lock);
  353. mutex_init(&adap->clist_lock);
  354. INIT_LIST_HEAD(&adap->clients);
  355. mutex_lock(&core_lock);
  356. /* Add the adapter to the driver core.
  357. * If the parent pointer is not set up,
  358. * we add this adapter to the host bus.
  359. */
  360. if (adap->dev.parent == NULL) {
  361. adap->dev.parent = &platform_bus;
  362. pr_debug("I2C adapter driver [%s] forgot to specify "
  363. "physical device\n", adap->name);
  364. }
  365. sprintf(adap->dev.bus_id, "i2c-%d", adap->nr);
  366. adap->dev.release = &i2c_adapter_dev_release;
  367. adap->dev.class = &i2c_adapter_class;
  368. res = device_register(&adap->dev);
  369. if (res)
  370. goto out_list;
  371. dev_dbg(&adap->dev, "adapter [%s] registered\n", adap->name);
  372. /* create pre-declared device nodes for new-style drivers */
  373. if (adap->nr < __i2c_first_dynamic_bus_num)
  374. i2c_scan_static_board_info(adap);
  375. /* let legacy drivers scan this bus for matching devices */
  376. dummy = bus_for_each_drv(&i2c_bus_type, NULL, adap,
  377. i2c_do_add_adapter);
  378. out_unlock:
  379. mutex_unlock(&core_lock);
  380. return res;
  381. out_list:
  382. idr_remove(&i2c_adapter_idr, adap->nr);
  383. goto out_unlock;
  384. }
  385. /**
  386. * i2c_add_adapter - declare i2c adapter, use dynamic bus number
  387. * @adapter: the adapter to add
  388. * Context: can sleep
  389. *
  390. * This routine is used to declare an I2C adapter when its bus number
  391. * doesn't matter. Examples: for I2C adapters dynamically added by
  392. * USB links or PCI plugin cards.
  393. *
  394. * When this returns zero, a new bus number was allocated and stored
  395. * in adap->nr, and the specified adapter became available for clients.
  396. * Otherwise, a negative errno value is returned.
  397. */
  398. int i2c_add_adapter(struct i2c_adapter *adapter)
  399. {
  400. int id, res = 0;
  401. retry:
  402. if (idr_pre_get(&i2c_adapter_idr, GFP_KERNEL) == 0)
  403. return -ENOMEM;
  404. mutex_lock(&core_lock);
  405. /* "above" here means "above or equal to", sigh */
  406. res = idr_get_new_above(&i2c_adapter_idr, adapter,
  407. __i2c_first_dynamic_bus_num, &id);
  408. mutex_unlock(&core_lock);
  409. if (res < 0) {
  410. if (res == -EAGAIN)
  411. goto retry;
  412. return res;
  413. }
  414. adapter->nr = id;
  415. return i2c_register_adapter(adapter);
  416. }
  417. EXPORT_SYMBOL(i2c_add_adapter);
  418. /**
  419. * i2c_add_numbered_adapter - declare i2c adapter, use static bus number
  420. * @adap: the adapter to register (with adap->nr initialized)
  421. * Context: can sleep
  422. *
  423. * This routine is used to declare an I2C adapter when its bus number
  424. * matters. For example, use it for I2C adapters from system-on-chip CPUs,
  425. * or otherwise built in to the system's mainboard, and where i2c_board_info
  426. * is used to properly configure I2C devices.
  427. *
  428. * If no devices have pre-been declared for this bus, then be sure to
  429. * register the adapter before any dynamically allocated ones. Otherwise
  430. * the required bus ID may not be available.
  431. *
  432. * When this returns zero, the specified adapter became available for
  433. * clients using the bus number provided in adap->nr. Also, the table
  434. * of I2C devices pre-declared using i2c_register_board_info() is scanned,
  435. * and the appropriate driver model device nodes are created. Otherwise, a
  436. * negative errno value is returned.
  437. */
  438. int i2c_add_numbered_adapter(struct i2c_adapter *adap)
  439. {
  440. int id;
  441. int status;
  442. if (adap->nr & ~MAX_ID_MASK)
  443. return -EINVAL;
  444. retry:
  445. if (idr_pre_get(&i2c_adapter_idr, GFP_KERNEL) == 0)
  446. return -ENOMEM;
  447. mutex_lock(&core_lock);
  448. /* "above" here means "above or equal to", sigh;
  449. * we need the "equal to" result to force the result
  450. */
  451. status = idr_get_new_above(&i2c_adapter_idr, adap, adap->nr, &id);
  452. if (status == 0 && id != adap->nr) {
  453. status = -EBUSY;
  454. idr_remove(&i2c_adapter_idr, id);
  455. }
  456. mutex_unlock(&core_lock);
  457. if (status == -EAGAIN)
  458. goto retry;
  459. if (status == 0)
  460. status = i2c_register_adapter(adap);
  461. return status;
  462. }
  463. EXPORT_SYMBOL_GPL(i2c_add_numbered_adapter);
  464. static int i2c_do_del_adapter(struct device_driver *d, void *data)
  465. {
  466. struct i2c_driver *driver = to_i2c_driver(d);
  467. struct i2c_adapter *adapter = data;
  468. int res;
  469. if (!driver->detach_adapter)
  470. return 0;
  471. res = driver->detach_adapter(adapter);
  472. if (res)
  473. dev_err(&adapter->dev, "detach_adapter failed (%d) "
  474. "for driver [%s]\n", res, driver->driver.name);
  475. return res;
  476. }
  477. /**
  478. * i2c_del_adapter - unregister I2C adapter
  479. * @adap: the adapter being unregistered
  480. * Context: can sleep
  481. *
  482. * This unregisters an I2C adapter which was previously registered
  483. * by @i2c_add_adapter or @i2c_add_numbered_adapter.
  484. */
  485. int i2c_del_adapter(struct i2c_adapter *adap)
  486. {
  487. struct i2c_client *client, *_n;
  488. int res = 0;
  489. mutex_lock(&core_lock);
  490. /* First make sure that this adapter was ever added */
  491. if (idr_find(&i2c_adapter_idr, adap->nr) != adap) {
  492. pr_debug("i2c-core: attempting to delete unregistered "
  493. "adapter [%s]\n", adap->name);
  494. res = -EINVAL;
  495. goto out_unlock;
  496. }
  497. /* Tell drivers about this removal */
  498. res = bus_for_each_drv(&i2c_bus_type, NULL, adap,
  499. i2c_do_del_adapter);
  500. if (res)
  501. goto out_unlock;
  502. /* detach any active clients. This must be done first, because
  503. * it can fail; in which case we give up. */
  504. list_for_each_entry_safe(client, _n, &adap->clients, list) {
  505. struct i2c_driver *driver;
  506. driver = client->driver;
  507. /* new style, follow standard driver model */
  508. if (!driver || is_newstyle_driver(driver)) {
  509. i2c_unregister_device(client);
  510. continue;
  511. }
  512. /* legacy drivers create and remove clients themselves */
  513. if ((res = driver->detach_client(client))) {
  514. dev_err(&adap->dev, "detach_client failed for client "
  515. "[%s] at address 0x%02x\n", client->name,
  516. client->addr);
  517. goto out_unlock;
  518. }
  519. }
  520. /* clean up the sysfs representation */
  521. init_completion(&adap->dev_released);
  522. device_unregister(&adap->dev);
  523. /* wait for sysfs to drop all references */
  524. wait_for_completion(&adap->dev_released);
  525. /* free bus id */
  526. idr_remove(&i2c_adapter_idr, adap->nr);
  527. dev_dbg(&adap->dev, "adapter [%s] unregistered\n", adap->name);
  528. out_unlock:
  529. mutex_unlock(&core_lock);
  530. return res;
  531. }
  532. EXPORT_SYMBOL(i2c_del_adapter);
  533. /* ------------------------------------------------------------------------- */
  534. static int __attach_adapter(struct device *dev, void *data)
  535. {
  536. struct i2c_adapter *adapter = to_i2c_adapter(dev);
  537. struct i2c_driver *driver = data;
  538. driver->attach_adapter(adapter);
  539. return 0;
  540. }
  541. /*
  542. * An i2c_driver is used with one or more i2c_client (device) nodes to access
  543. * i2c slave chips, on a bus instance associated with some i2c_adapter. There
  544. * are two models for binding the driver to its device: "new style" drivers
  545. * follow the standard Linux driver model and just respond to probe() calls
  546. * issued if the driver core sees they match(); "legacy" drivers create device
  547. * nodes themselves.
  548. */
  549. int i2c_register_driver(struct module *owner, struct i2c_driver *driver)
  550. {
  551. int res;
  552. /* new style driver methods can't mix with legacy ones */
  553. if (is_newstyle_driver(driver)) {
  554. if (driver->attach_adapter || driver->detach_adapter
  555. || driver->detach_client) {
  556. printk(KERN_WARNING
  557. "i2c-core: driver [%s] is confused\n",
  558. driver->driver.name);
  559. return -EINVAL;
  560. }
  561. }
  562. /* add the driver to the list of i2c drivers in the driver core */
  563. driver->driver.owner = owner;
  564. driver->driver.bus = &i2c_bus_type;
  565. /* for new style drivers, when registration returns the driver core
  566. * will have called probe() for all matching-but-unbound devices.
  567. */
  568. res = driver_register(&driver->driver);
  569. if (res)
  570. return res;
  571. mutex_lock(&core_lock);
  572. pr_debug("i2c-core: driver [%s] registered\n", driver->driver.name);
  573. /* legacy drivers scan i2c busses directly */
  574. if (driver->attach_adapter)
  575. class_for_each_device(&i2c_adapter_class, driver,
  576. __attach_adapter);
  577. mutex_unlock(&core_lock);
  578. return 0;
  579. }
  580. EXPORT_SYMBOL(i2c_register_driver);
  581. static int __detach_adapter(struct device *dev, void *data)
  582. {
  583. struct i2c_adapter *adapter = to_i2c_adapter(dev);
  584. struct i2c_driver *driver = data;
  585. /* Have a look at each adapter, if clients of this driver are still
  586. * attached. If so, detach them to be able to kill the driver
  587. * afterwards.
  588. */
  589. if (driver->detach_adapter) {
  590. if (driver->detach_adapter(adapter))
  591. dev_err(&adapter->dev,
  592. "detach_adapter failed for driver [%s]\n",
  593. driver->driver.name);
  594. } else {
  595. struct i2c_client *client, *_n;
  596. list_for_each_entry_safe(client, _n, &adapter->clients, list) {
  597. if (client->driver != driver)
  598. continue;
  599. dev_dbg(&adapter->dev,
  600. "detaching client [%s] at 0x%02x\n",
  601. client->name, client->addr);
  602. if (driver->detach_client(client))
  603. dev_err(&adapter->dev, "detach_client "
  604. "failed for client [%s] at 0x%02x\n",
  605. client->name, client->addr);
  606. }
  607. }
  608. return 0;
  609. }
  610. /**
  611. * i2c_del_driver - unregister I2C driver
  612. * @driver: the driver being unregistered
  613. * Context: can sleep
  614. */
  615. void i2c_del_driver(struct i2c_driver *driver)
  616. {
  617. mutex_lock(&core_lock);
  618. /* legacy driver? */
  619. if (!is_newstyle_driver(driver))
  620. class_for_each_device(&i2c_adapter_class, driver,
  621. __detach_adapter);
  622. driver_unregister(&driver->driver);
  623. pr_debug("i2c-core: driver [%s] unregistered\n", driver->driver.name);
  624. mutex_unlock(&core_lock);
  625. }
  626. EXPORT_SYMBOL(i2c_del_driver);
  627. /* ------------------------------------------------------------------------- */
  628. static int __i2c_check_addr(struct device *dev, void *addrp)
  629. {
  630. struct i2c_client *client = i2c_verify_client(dev);
  631. int addr = *(int *)addrp;
  632. if (client && client->addr == addr)
  633. return -EBUSY;
  634. return 0;
  635. }
  636. static int i2c_check_addr(struct i2c_adapter *adapter, int addr)
  637. {
  638. return device_for_each_child(&adapter->dev, &addr, __i2c_check_addr);
  639. }
  640. int i2c_attach_client(struct i2c_client *client)
  641. {
  642. struct i2c_adapter *adapter = client->adapter;
  643. int res = 0;
  644. client->dev.parent = &client->adapter->dev;
  645. client->dev.bus = &i2c_bus_type;
  646. if (client->driver)
  647. client->dev.driver = &client->driver->driver;
  648. if (client->driver && !is_newstyle_driver(client->driver)) {
  649. client->dev.release = i2c_client_release;
  650. client->dev.uevent_suppress = 1;
  651. } else
  652. client->dev.release = i2c_client_dev_release;
  653. snprintf(&client->dev.bus_id[0], sizeof(client->dev.bus_id),
  654. "%d-%04x", i2c_adapter_id(adapter), client->addr);
  655. res = device_register(&client->dev);
  656. if (res)
  657. goto out_err;
  658. mutex_lock(&adapter->clist_lock);
  659. list_add_tail(&client->list, &adapter->clients);
  660. mutex_unlock(&adapter->clist_lock);
  661. dev_dbg(&adapter->dev, "client [%s] registered with bus id %s\n",
  662. client->name, client->dev.bus_id);
  663. if (adapter->client_register) {
  664. if (adapter->client_register(client)) {
  665. dev_dbg(&adapter->dev, "client_register "
  666. "failed for client [%s] at 0x%02x\n",
  667. client->name, client->addr);
  668. }
  669. }
  670. return 0;
  671. out_err:
  672. dev_err(&adapter->dev, "Failed to attach i2c client %s at 0x%02x "
  673. "(%d)\n", client->name, client->addr, res);
  674. return res;
  675. }
  676. EXPORT_SYMBOL(i2c_attach_client);
  677. int i2c_detach_client(struct i2c_client *client)
  678. {
  679. struct i2c_adapter *adapter = client->adapter;
  680. int res = 0;
  681. if (adapter->client_unregister) {
  682. res = adapter->client_unregister(client);
  683. if (res) {
  684. dev_err(&client->dev,
  685. "client_unregister [%s] failed, "
  686. "client not detached\n", client->name);
  687. goto out;
  688. }
  689. }
  690. mutex_lock(&adapter->clist_lock);
  691. list_del(&client->list);
  692. mutex_unlock(&adapter->clist_lock);
  693. init_completion(&client->released);
  694. device_unregister(&client->dev);
  695. wait_for_completion(&client->released);
  696. out:
  697. return res;
  698. }
  699. EXPORT_SYMBOL(i2c_detach_client);
  700. /**
  701. * i2c_use_client - increments the reference count of the i2c client structure
  702. * @client: the client being referenced
  703. *
  704. * Each live reference to a client should be refcounted. The driver model does
  705. * that automatically as part of driver binding, so that most drivers don't
  706. * need to do this explicitly: they hold a reference until they're unbound
  707. * from the device.
  708. *
  709. * A pointer to the client with the incremented reference counter is returned.
  710. */
  711. struct i2c_client *i2c_use_client(struct i2c_client *client)
  712. {
  713. if (client && get_device(&client->dev))
  714. return client;
  715. return NULL;
  716. }
  717. EXPORT_SYMBOL(i2c_use_client);
  718. /**
  719. * i2c_release_client - release a use of the i2c client structure
  720. * @client: the client being no longer referenced
  721. *
  722. * Must be called when a user of a client is finished with it.
  723. */
  724. void i2c_release_client(struct i2c_client *client)
  725. {
  726. if (client)
  727. put_device(&client->dev);
  728. }
  729. EXPORT_SYMBOL(i2c_release_client);
  730. struct i2c_cmd_arg {
  731. unsigned cmd;
  732. void *arg;
  733. };
  734. static int i2c_cmd(struct device *dev, void *_arg)
  735. {
  736. struct i2c_client *client = i2c_verify_client(dev);
  737. struct i2c_cmd_arg *arg = _arg;
  738. if (client && client->driver && client->driver->command)
  739. client->driver->command(client, arg->cmd, arg->arg);
  740. return 0;
  741. }
  742. void i2c_clients_command(struct i2c_adapter *adap, unsigned int cmd, void *arg)
  743. {
  744. struct i2c_cmd_arg cmd_arg;
  745. cmd_arg.cmd = cmd;
  746. cmd_arg.arg = arg;
  747. device_for_each_child(&adap->dev, &cmd_arg, i2c_cmd);
  748. }
  749. EXPORT_SYMBOL(i2c_clients_command);
  750. static int __init i2c_init(void)
  751. {
  752. int retval;
  753. retval = bus_register(&i2c_bus_type);
  754. if (retval)
  755. return retval;
  756. retval = class_register(&i2c_adapter_class);
  757. if (retval)
  758. goto bus_err;
  759. retval = i2c_add_driver(&dummy_driver);
  760. if (retval)
  761. goto class_err;
  762. return 0;
  763. class_err:
  764. class_unregister(&i2c_adapter_class);
  765. bus_err:
  766. bus_unregister(&i2c_bus_type);
  767. return retval;
  768. }
  769. static void __exit i2c_exit(void)
  770. {
  771. i2c_del_driver(&dummy_driver);
  772. class_unregister(&i2c_adapter_class);
  773. bus_unregister(&i2c_bus_type);
  774. }
  775. subsys_initcall(i2c_init);
  776. module_exit(i2c_exit);
  777. /* ----------------------------------------------------
  778. * the functional interface to the i2c busses.
  779. * ----------------------------------------------------
  780. */
  781. /**
  782. * i2c_transfer - execute a single or combined I2C message
  783. * @adap: Handle to I2C bus
  784. * @msgs: One or more messages to execute before STOP is issued to
  785. * terminate the operation; each message begins with a START.
  786. * @num: Number of messages to be executed.
  787. *
  788. * Returns negative errno, else the number of messages executed.
  789. *
  790. * Note that there is no requirement that each message be sent to
  791. * the same slave address, although that is the most common model.
  792. */
  793. int i2c_transfer(struct i2c_adapter * adap, struct i2c_msg *msgs, int num)
  794. {
  795. int ret;
  796. /* REVISIT the fault reporting model here is weak:
  797. *
  798. * - When we get an error after receiving N bytes from a slave,
  799. * there is no way to report "N".
  800. *
  801. * - When we get a NAK after transmitting N bytes to a slave,
  802. * there is no way to report "N" ... or to let the master
  803. * continue executing the rest of this combined message, if
  804. * that's the appropriate response.
  805. *
  806. * - When for example "num" is two and we successfully complete
  807. * the first message but get an error part way through the
  808. * second, it's unclear whether that should be reported as
  809. * one (discarding status on the second message) or errno
  810. * (discarding status on the first one).
  811. */
  812. if (adap->algo->master_xfer) {
  813. #ifdef DEBUG
  814. for (ret = 0; ret < num; ret++) {
  815. dev_dbg(&adap->dev, "master_xfer[%d] %c, addr=0x%02x, "
  816. "len=%d%s\n", ret, (msgs[ret].flags & I2C_M_RD)
  817. ? 'R' : 'W', msgs[ret].addr, msgs[ret].len,
  818. (msgs[ret].flags & I2C_M_RECV_LEN) ? "+" : "");
  819. }
  820. #endif
  821. if (in_atomic() || irqs_disabled()) {
  822. ret = mutex_trylock(&adap->bus_lock);
  823. if (!ret)
  824. /* I2C activity is ongoing. */
  825. return -EAGAIN;
  826. } else {
  827. mutex_lock_nested(&adap->bus_lock, adap->level);
  828. }
  829. ret = adap->algo->master_xfer(adap,msgs,num);
  830. mutex_unlock(&adap->bus_lock);
  831. return ret;
  832. } else {
  833. dev_dbg(&adap->dev, "I2C level transfers not supported\n");
  834. return -EOPNOTSUPP;
  835. }
  836. }
  837. EXPORT_SYMBOL(i2c_transfer);
  838. /**
  839. * i2c_master_send - issue a single I2C message in master transmit mode
  840. * @client: Handle to slave device
  841. * @buf: Data that will be written to the slave
  842. * @count: How many bytes to write
  843. *
  844. * Returns negative errno, or else the number of bytes written.
  845. */
  846. int i2c_master_send(struct i2c_client *client,const char *buf ,int count)
  847. {
  848. int ret;
  849. struct i2c_adapter *adap=client->adapter;
  850. struct i2c_msg msg;
  851. msg.addr = client->addr;
  852. msg.flags = client->flags & I2C_M_TEN;
  853. msg.len = count;
  854. msg.buf = (char *)buf;
  855. ret = i2c_transfer(adap, &msg, 1);
  856. /* If everything went ok (i.e. 1 msg transmitted), return #bytes
  857. transmitted, else error code. */
  858. return (ret == 1) ? count : ret;
  859. }
  860. EXPORT_SYMBOL(i2c_master_send);
  861. /**
  862. * i2c_master_recv - issue a single I2C message in master receive mode
  863. * @client: Handle to slave device
  864. * @buf: Where to store data read from slave
  865. * @count: How many bytes to read
  866. *
  867. * Returns negative errno, or else the number of bytes read.
  868. */
  869. int i2c_master_recv(struct i2c_client *client, char *buf ,int count)
  870. {
  871. struct i2c_adapter *adap=client->adapter;
  872. struct i2c_msg msg;
  873. int ret;
  874. msg.addr = client->addr;
  875. msg.flags = client->flags & I2C_M_TEN;
  876. msg.flags |= I2C_M_RD;
  877. msg.len = count;
  878. msg.buf = buf;
  879. ret = i2c_transfer(adap, &msg, 1);
  880. /* If everything went ok (i.e. 1 msg transmitted), return #bytes
  881. transmitted, else error code. */
  882. return (ret == 1) ? count : ret;
  883. }
  884. EXPORT_SYMBOL(i2c_master_recv);
  885. /* ----------------------------------------------------
  886. * the i2c address scanning function
  887. * Will not work for 10-bit addresses!
  888. * ----------------------------------------------------
  889. */
  890. static int i2c_probe_address(struct i2c_adapter *adapter, int addr, int kind,
  891. int (*found_proc) (struct i2c_adapter *, int, int))
  892. {
  893. int err;
  894. /* Make sure the address is valid */
  895. if (addr < 0x03 || addr > 0x77) {
  896. dev_warn(&adapter->dev, "Invalid probe address 0x%02x\n",
  897. addr);
  898. return -EINVAL;
  899. }
  900. /* Skip if already in use */
  901. if (i2c_check_addr(adapter, addr))
  902. return 0;
  903. /* Make sure there is something at this address, unless forced */
  904. if (kind < 0) {
  905. if (i2c_smbus_xfer(adapter, addr, 0, 0, 0,
  906. I2C_SMBUS_QUICK, NULL) < 0)
  907. return 0;
  908. /* prevent 24RF08 corruption */
  909. if ((addr & ~0x0f) == 0x50)
  910. i2c_smbus_xfer(adapter, addr, 0, 0, 0,
  911. I2C_SMBUS_QUICK, NULL);
  912. }
  913. /* Finally call the custom detection function */
  914. err = found_proc(adapter, addr, kind);
  915. /* -ENODEV can be returned if there is a chip at the given address
  916. but it isn't supported by this chip driver. We catch it here as
  917. this isn't an error. */
  918. if (err == -ENODEV)
  919. err = 0;
  920. if (err)
  921. dev_warn(&adapter->dev, "Client creation failed at 0x%x (%d)\n",
  922. addr, err);
  923. return err;
  924. }
  925. int i2c_probe(struct i2c_adapter *adapter,
  926. const struct i2c_client_address_data *address_data,
  927. int (*found_proc) (struct i2c_adapter *, int, int))
  928. {
  929. int i, err;
  930. int adap_id = i2c_adapter_id(adapter);
  931. /* Force entries are done first, and are not affected by ignore
  932. entries */
  933. if (address_data->forces) {
  934. const unsigned short * const *forces = address_data->forces;
  935. int kind;
  936. for (kind = 0; forces[kind]; kind++) {
  937. for (i = 0; forces[kind][i] != I2C_CLIENT_END;
  938. i += 2) {
  939. if (forces[kind][i] == adap_id
  940. || forces[kind][i] == ANY_I2C_BUS) {
  941. dev_dbg(&adapter->dev, "found force "
  942. "parameter for adapter %d, "
  943. "addr 0x%02x, kind %d\n",
  944. adap_id, forces[kind][i + 1],
  945. kind);
  946. err = i2c_probe_address(adapter,
  947. forces[kind][i + 1],
  948. kind, found_proc);
  949. if (err)
  950. return err;
  951. }
  952. }
  953. }
  954. }
  955. /* Stop here if we can't use SMBUS_QUICK */
  956. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_QUICK)) {
  957. if (address_data->probe[0] == I2C_CLIENT_END
  958. && address_data->normal_i2c[0] == I2C_CLIENT_END)
  959. return 0;
  960. dev_warn(&adapter->dev, "SMBus Quick command not supported, "
  961. "can't probe for chips\n");
  962. return -EOPNOTSUPP;
  963. }
  964. /* Probe entries are done second, and are not affected by ignore
  965. entries either */
  966. for (i = 0; address_data->probe[i] != I2C_CLIENT_END; i += 2) {
  967. if (address_data->probe[i] == adap_id
  968. || address_data->probe[i] == ANY_I2C_BUS) {
  969. dev_dbg(&adapter->dev, "found probe parameter for "
  970. "adapter %d, addr 0x%02x\n", adap_id,
  971. address_data->probe[i + 1]);
  972. err = i2c_probe_address(adapter,
  973. address_data->probe[i + 1],
  974. -1, found_proc);
  975. if (err)
  976. return err;
  977. }
  978. }
  979. /* Normal entries are done last, unless shadowed by an ignore entry */
  980. for (i = 0; address_data->normal_i2c[i] != I2C_CLIENT_END; i += 1) {
  981. int j, ignore;
  982. ignore = 0;
  983. for (j = 0; address_data->ignore[j] != I2C_CLIENT_END;
  984. j += 2) {
  985. if ((address_data->ignore[j] == adap_id ||
  986. address_data->ignore[j] == ANY_I2C_BUS)
  987. && address_data->ignore[j + 1]
  988. == address_data->normal_i2c[i]) {
  989. dev_dbg(&adapter->dev, "found ignore "
  990. "parameter for adapter %d, "
  991. "addr 0x%02x\n", adap_id,
  992. address_data->ignore[j + 1]);
  993. ignore = 1;
  994. break;
  995. }
  996. }
  997. if (ignore)
  998. continue;
  999. dev_dbg(&adapter->dev, "found normal entry for adapter %d, "
  1000. "addr 0x%02x\n", adap_id,
  1001. address_data->normal_i2c[i]);
  1002. err = i2c_probe_address(adapter, address_data->normal_i2c[i],
  1003. -1, found_proc);
  1004. if (err)
  1005. return err;
  1006. }
  1007. return 0;
  1008. }
  1009. EXPORT_SYMBOL(i2c_probe);
  1010. struct i2c_client *
  1011. i2c_new_probed_device(struct i2c_adapter *adap,
  1012. struct i2c_board_info *info,
  1013. unsigned short const *addr_list)
  1014. {
  1015. int i;
  1016. /* Stop here if the bus doesn't support probing */
  1017. if (!i2c_check_functionality(adap, I2C_FUNC_SMBUS_READ_BYTE)) {
  1018. dev_err(&adap->dev, "Probing not supported\n");
  1019. return NULL;
  1020. }
  1021. for (i = 0; addr_list[i] != I2C_CLIENT_END; i++) {
  1022. /* Check address validity */
  1023. if (addr_list[i] < 0x03 || addr_list[i] > 0x77) {
  1024. dev_warn(&adap->dev, "Invalid 7-bit address "
  1025. "0x%02x\n", addr_list[i]);
  1026. continue;
  1027. }
  1028. /* Check address availability */
  1029. if (i2c_check_addr(adap, addr_list[i])) {
  1030. dev_dbg(&adap->dev, "Address 0x%02x already in "
  1031. "use, not probing\n", addr_list[i]);
  1032. continue;
  1033. }
  1034. /* Test address responsiveness
  1035. The default probe method is a quick write, but it is known
  1036. to corrupt the 24RF08 EEPROMs due to a state machine bug,
  1037. and could also irreversibly write-protect some EEPROMs, so
  1038. for address ranges 0x30-0x37 and 0x50-0x5f, we use a byte
  1039. read instead. Also, some bus drivers don't implement
  1040. quick write, so we fallback to a byte read it that case
  1041. too. */
  1042. if ((addr_list[i] & ~0x07) == 0x30
  1043. || (addr_list[i] & ~0x0f) == 0x50
  1044. || !i2c_check_functionality(adap, I2C_FUNC_SMBUS_QUICK)) {
  1045. if (i2c_smbus_xfer(adap, addr_list[i], 0,
  1046. I2C_SMBUS_READ, 0,
  1047. I2C_SMBUS_BYTE, NULL) >= 0)
  1048. break;
  1049. } else {
  1050. if (i2c_smbus_xfer(adap, addr_list[i], 0,
  1051. I2C_SMBUS_WRITE, 0,
  1052. I2C_SMBUS_QUICK, NULL) >= 0)
  1053. break;
  1054. }
  1055. }
  1056. if (addr_list[i] == I2C_CLIENT_END) {
  1057. dev_dbg(&adap->dev, "Probing failed, no device found\n");
  1058. return NULL;
  1059. }
  1060. info->addr = addr_list[i];
  1061. return i2c_new_device(adap, info);
  1062. }
  1063. EXPORT_SYMBOL_GPL(i2c_new_probed_device);
  1064. struct i2c_adapter* i2c_get_adapter(int id)
  1065. {
  1066. struct i2c_adapter *adapter;
  1067. mutex_lock(&core_lock);
  1068. adapter = (struct i2c_adapter *)idr_find(&i2c_adapter_idr, id);
  1069. if (adapter && !try_module_get(adapter->owner))
  1070. adapter = NULL;
  1071. mutex_unlock(&core_lock);
  1072. return adapter;
  1073. }
  1074. EXPORT_SYMBOL(i2c_get_adapter);
  1075. void i2c_put_adapter(struct i2c_adapter *adap)
  1076. {
  1077. module_put(adap->owner);
  1078. }
  1079. EXPORT_SYMBOL(i2c_put_adapter);
  1080. /* The SMBus parts */
  1081. #define POLY (0x1070U << 3)
  1082. static u8
  1083. crc8(u16 data)
  1084. {
  1085. int i;
  1086. for(i = 0; i < 8; i++) {
  1087. if (data & 0x8000)
  1088. data = data ^ POLY;
  1089. data = data << 1;
  1090. }
  1091. return (u8)(data >> 8);
  1092. }
  1093. /* Incremental CRC8 over count bytes in the array pointed to by p */
  1094. static u8 i2c_smbus_pec(u8 crc, u8 *p, size_t count)
  1095. {
  1096. int i;
  1097. for(i = 0; i < count; i++)
  1098. crc = crc8((crc ^ p[i]) << 8);
  1099. return crc;
  1100. }
  1101. /* Assume a 7-bit address, which is reasonable for SMBus */
  1102. static u8 i2c_smbus_msg_pec(u8 pec, struct i2c_msg *msg)
  1103. {
  1104. /* The address will be sent first */
  1105. u8 addr = (msg->addr << 1) | !!(msg->flags & I2C_M_RD);
  1106. pec = i2c_smbus_pec(pec, &addr, 1);
  1107. /* The data buffer follows */
  1108. return i2c_smbus_pec(pec, msg->buf, msg->len);
  1109. }
  1110. /* Used for write only transactions */
  1111. static inline void i2c_smbus_add_pec(struct i2c_msg *msg)
  1112. {
  1113. msg->buf[msg->len] = i2c_smbus_msg_pec(0, msg);
  1114. msg->len++;
  1115. }
  1116. /* Return <0 on CRC error
  1117. If there was a write before this read (most cases) we need to take the
  1118. partial CRC from the write part into account.
  1119. Note that this function does modify the message (we need to decrease the
  1120. message length to hide the CRC byte from the caller). */
  1121. static int i2c_smbus_check_pec(u8 cpec, struct i2c_msg *msg)
  1122. {
  1123. u8 rpec = msg->buf[--msg->len];
  1124. cpec = i2c_smbus_msg_pec(cpec, msg);
  1125. if (rpec != cpec) {
  1126. pr_debug("i2c-core: Bad PEC 0x%02x vs. 0x%02x\n",
  1127. rpec, cpec);
  1128. return -EBADMSG;
  1129. }
  1130. return 0;
  1131. }
  1132. /**
  1133. * i2c_smbus_read_byte - SMBus "receive byte" protocol
  1134. * @client: Handle to slave device
  1135. *
  1136. * This executes the SMBus "receive byte" protocol, returning negative errno
  1137. * else the byte received from the device.
  1138. */
  1139. s32 i2c_smbus_read_byte(struct i2c_client *client)
  1140. {
  1141. union i2c_smbus_data data;
  1142. int status;
  1143. status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
  1144. I2C_SMBUS_READ, 0,
  1145. I2C_SMBUS_BYTE, &data);
  1146. return (status < 0) ? status : data.byte;
  1147. }
  1148. EXPORT_SYMBOL(i2c_smbus_read_byte);
  1149. /**
  1150. * i2c_smbus_write_byte - SMBus "send byte" protocol
  1151. * @client: Handle to slave device
  1152. * @value: Byte to be sent
  1153. *
  1154. * This executes the SMBus "send byte" protocol, returning negative errno
  1155. * else zero on success.
  1156. */
  1157. s32 i2c_smbus_write_byte(struct i2c_client *client, u8 value)
  1158. {
  1159. return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
  1160. I2C_SMBUS_WRITE, value, I2C_SMBUS_BYTE, NULL);
  1161. }
  1162. EXPORT_SYMBOL(i2c_smbus_write_byte);
  1163. /**
  1164. * i2c_smbus_read_byte_data - SMBus "read byte" protocol
  1165. * @client: Handle to slave device
  1166. * @command: Byte interpreted by slave
  1167. *
  1168. * This executes the SMBus "read byte" protocol, returning negative errno
  1169. * else a data byte received from the device.
  1170. */
  1171. s32 i2c_smbus_read_byte_data(struct i2c_client *client, u8 command)
  1172. {
  1173. union i2c_smbus_data data;
  1174. int status;
  1175. status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
  1176. I2C_SMBUS_READ, command,
  1177. I2C_SMBUS_BYTE_DATA, &data);
  1178. return (status < 0) ? status : data.byte;
  1179. }
  1180. EXPORT_SYMBOL(i2c_smbus_read_byte_data);
  1181. /**
  1182. * i2c_smbus_write_byte_data - SMBus "write byte" protocol
  1183. * @client: Handle to slave device
  1184. * @command: Byte interpreted by slave
  1185. * @value: Byte being written
  1186. *
  1187. * This executes the SMBus "write byte" protocol, returning negative errno
  1188. * else zero on success.
  1189. */
  1190. s32 i2c_smbus_write_byte_data(struct i2c_client *client, u8 command, u8 value)
  1191. {
  1192. union i2c_smbus_data data;
  1193. data.byte = value;
  1194. return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
  1195. I2C_SMBUS_WRITE,command,
  1196. I2C_SMBUS_BYTE_DATA,&data);
  1197. }
  1198. EXPORT_SYMBOL(i2c_smbus_write_byte_data);
  1199. /**
  1200. * i2c_smbus_read_word_data - SMBus "read word" protocol
  1201. * @client: Handle to slave device
  1202. * @command: Byte interpreted by slave
  1203. *
  1204. * This executes the SMBus "read word" protocol, returning negative errno
  1205. * else a 16-bit unsigned "word" received from the device.
  1206. */
  1207. s32 i2c_smbus_read_word_data(struct i2c_client *client, u8 command)
  1208. {
  1209. union i2c_smbus_data data;
  1210. int status;
  1211. status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
  1212. I2C_SMBUS_READ, command,
  1213. I2C_SMBUS_WORD_DATA, &data);
  1214. return (status < 0) ? status : data.word;
  1215. }
  1216. EXPORT_SYMBOL(i2c_smbus_read_word_data);
  1217. /**
  1218. * i2c_smbus_write_word_data - SMBus "write word" protocol
  1219. * @client: Handle to slave device
  1220. * @command: Byte interpreted by slave
  1221. * @value: 16-bit "word" being written
  1222. *
  1223. * This executes the SMBus "write word" protocol, returning negative errno
  1224. * else zero on success.
  1225. */
  1226. s32 i2c_smbus_write_word_data(struct i2c_client *client, u8 command, u16 value)
  1227. {
  1228. union i2c_smbus_data data;
  1229. data.word = value;
  1230. return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
  1231. I2C_SMBUS_WRITE,command,
  1232. I2C_SMBUS_WORD_DATA,&data);
  1233. }
  1234. EXPORT_SYMBOL(i2c_smbus_write_word_data);
  1235. /**
  1236. * i2c_smbus_read_block_data - SMBus "block read" protocol
  1237. * @client: Handle to slave device
  1238. * @command: Byte interpreted by slave
  1239. * @values: Byte array into which data will be read; big enough to hold
  1240. * the data returned by the slave. SMBus allows at most 32 bytes.
  1241. *
  1242. * This executes the SMBus "block read" protocol, returning negative errno
  1243. * else the number of data bytes in the slave's response.
  1244. *
  1245. * Note that using this function requires that the client's adapter support
  1246. * the I2C_FUNC_SMBUS_READ_BLOCK_DATA functionality. Not all adapter drivers
  1247. * support this; its emulation through I2C messaging relies on a specific
  1248. * mechanism (I2C_M_RECV_LEN) which may not be implemented.
  1249. */
  1250. s32 i2c_smbus_read_block_data(struct i2c_client *client, u8 command,
  1251. u8 *values)
  1252. {
  1253. union i2c_smbus_data data;
  1254. int status;
  1255. status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
  1256. I2C_SMBUS_READ, command,
  1257. I2C_SMBUS_BLOCK_DATA, &data);
  1258. if (status)
  1259. return status;
  1260. memcpy(values, &data.block[1], data.block[0]);
  1261. return data.block[0];
  1262. }
  1263. EXPORT_SYMBOL(i2c_smbus_read_block_data);
  1264. /**
  1265. * i2c_smbus_write_block_data - SMBus "block write" protocol
  1266. * @client: Handle to slave device
  1267. * @command: Byte interpreted by slave
  1268. * @length: Size of data block; SMBus allows at most 32 bytes
  1269. * @values: Byte array which will be written.
  1270. *
  1271. * This executes the SMBus "block write" protocol, returning negative errno
  1272. * else zero on success.
  1273. */
  1274. s32 i2c_smbus_write_block_data(struct i2c_client *client, u8 command,
  1275. u8 length, const u8 *values)
  1276. {
  1277. union i2c_smbus_data data;
  1278. if (length > I2C_SMBUS_BLOCK_MAX)
  1279. length = I2C_SMBUS_BLOCK_MAX;
  1280. data.block[0] = length;
  1281. memcpy(&data.block[1], values, length);
  1282. return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
  1283. I2C_SMBUS_WRITE,command,
  1284. I2C_SMBUS_BLOCK_DATA,&data);
  1285. }
  1286. EXPORT_SYMBOL(i2c_smbus_write_block_data);
  1287. /* Returns the number of read bytes */
  1288. s32 i2c_smbus_read_i2c_block_data(struct i2c_client *client, u8 command,
  1289. u8 length, u8 *values)
  1290. {
  1291. union i2c_smbus_data data;
  1292. int status;
  1293. if (length > I2C_SMBUS_BLOCK_MAX)
  1294. length = I2C_SMBUS_BLOCK_MAX;
  1295. data.block[0] = length;
  1296. status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
  1297. I2C_SMBUS_READ, command,
  1298. I2C_SMBUS_I2C_BLOCK_DATA, &data);
  1299. if (status < 0)
  1300. return status;
  1301. memcpy(values, &data.block[1], data.block[0]);
  1302. return data.block[0];
  1303. }
  1304. EXPORT_SYMBOL(i2c_smbus_read_i2c_block_data);
  1305. s32 i2c_smbus_write_i2c_block_data(struct i2c_client *client, u8 command,
  1306. u8 length, const u8 *values)
  1307. {
  1308. union i2c_smbus_data data;
  1309. if (length > I2C_SMBUS_BLOCK_MAX)
  1310. length = I2C_SMBUS_BLOCK_MAX;
  1311. data.block[0] = length;
  1312. memcpy(data.block + 1, values, length);
  1313. return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
  1314. I2C_SMBUS_WRITE, command,
  1315. I2C_SMBUS_I2C_BLOCK_DATA, &data);
  1316. }
  1317. EXPORT_SYMBOL(i2c_smbus_write_i2c_block_data);
  1318. /* Simulate a SMBus command using the i2c protocol
  1319. No checking of parameters is done! */
  1320. static s32 i2c_smbus_xfer_emulated(struct i2c_adapter * adapter, u16 addr,
  1321. unsigned short flags,
  1322. char read_write, u8 command, int size,
  1323. union i2c_smbus_data * data)
  1324. {
  1325. /* So we need to generate a series of msgs. In the case of writing, we
  1326. need to use only one message; when reading, we need two. We initialize
  1327. most things with sane defaults, to keep the code below somewhat
  1328. simpler. */
  1329. unsigned char msgbuf0[I2C_SMBUS_BLOCK_MAX+3];
  1330. unsigned char msgbuf1[I2C_SMBUS_BLOCK_MAX+2];
  1331. int num = read_write == I2C_SMBUS_READ?2:1;
  1332. struct i2c_msg msg[2] = { { addr, flags, 1, msgbuf0 },
  1333. { addr, flags | I2C_M_RD, 0, msgbuf1 }
  1334. };
  1335. int i;
  1336. u8 partial_pec = 0;
  1337. int status;
  1338. msgbuf0[0] = command;
  1339. switch(size) {
  1340. case I2C_SMBUS_QUICK:
  1341. msg[0].len = 0;
  1342. /* Special case: The read/write field is used as data */
  1343. msg[0].flags = flags | (read_write==I2C_SMBUS_READ)?I2C_M_RD:0;
  1344. num = 1;
  1345. break;
  1346. case I2C_SMBUS_BYTE:
  1347. if (read_write == I2C_SMBUS_READ) {
  1348. /* Special case: only a read! */
  1349. msg[0].flags = I2C_M_RD | flags;
  1350. num = 1;
  1351. }
  1352. break;
  1353. case I2C_SMBUS_BYTE_DATA:
  1354. if (read_write == I2C_SMBUS_READ)
  1355. msg[1].len = 1;
  1356. else {
  1357. msg[0].len = 2;
  1358. msgbuf0[1] = data->byte;
  1359. }
  1360. break;
  1361. case I2C_SMBUS_WORD_DATA:
  1362. if (read_write == I2C_SMBUS_READ)
  1363. msg[1].len = 2;
  1364. else {
  1365. msg[0].len=3;
  1366. msgbuf0[1] = data->word & 0xff;
  1367. msgbuf0[2] = data->word >> 8;
  1368. }
  1369. break;
  1370. case I2C_SMBUS_PROC_CALL:
  1371. num = 2; /* Special case */
  1372. read_write = I2C_SMBUS_READ;
  1373. msg[0].len = 3;
  1374. msg[1].len = 2;
  1375. msgbuf0[1] = data->word & 0xff;
  1376. msgbuf0[2] = data->word >> 8;
  1377. break;
  1378. case I2C_SMBUS_BLOCK_DATA:
  1379. if (read_write == I2C_SMBUS_READ) {
  1380. msg[1].flags |= I2C_M_RECV_LEN;
  1381. msg[1].len = 1; /* block length will be added by
  1382. the underlying bus driver */
  1383. } else {
  1384. msg[0].len = data->block[0] + 2;
  1385. if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 2) {
  1386. dev_err(&adapter->dev,
  1387. "Invalid block write size %d\n",
  1388. data->block[0]);
  1389. return -EINVAL;
  1390. }
  1391. for (i = 1; i < msg[0].len; i++)
  1392. msgbuf0[i] = data->block[i-1];
  1393. }
  1394. break;
  1395. case I2C_SMBUS_BLOCK_PROC_CALL:
  1396. num = 2; /* Another special case */
  1397. read_write = I2C_SMBUS_READ;
  1398. if (data->block[0] > I2C_SMBUS_BLOCK_MAX) {
  1399. dev_err(&adapter->dev,
  1400. "Invalid block write size %d\n",
  1401. data->block[0]);
  1402. return -EINVAL;
  1403. }
  1404. msg[0].len = data->block[0] + 2;
  1405. for (i = 1; i < msg[0].len; i++)
  1406. msgbuf0[i] = data->block[i-1];
  1407. msg[1].flags |= I2C_M_RECV_LEN;
  1408. msg[1].len = 1; /* block length will be added by
  1409. the underlying bus driver */
  1410. break;
  1411. case I2C_SMBUS_I2C_BLOCK_DATA:
  1412. if (read_write == I2C_SMBUS_READ) {
  1413. msg[1].len = data->block[0];
  1414. } else {
  1415. msg[0].len = data->block[0] + 1;
  1416. if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 1) {
  1417. dev_err(&adapter->dev,
  1418. "Invalid block write size %d\n",
  1419. data->block[0]);
  1420. return -EINVAL;
  1421. }
  1422. for (i = 1; i <= data->block[0]; i++)
  1423. msgbuf0[i] = data->block[i];
  1424. }
  1425. break;
  1426. default:
  1427. dev_err(&adapter->dev, "Unsupported transaction %d\n", size);
  1428. return -EOPNOTSUPP;
  1429. }
  1430. i = ((flags & I2C_CLIENT_PEC) && size != I2C_SMBUS_QUICK
  1431. && size != I2C_SMBUS_I2C_BLOCK_DATA);
  1432. if (i) {
  1433. /* Compute PEC if first message is a write */
  1434. if (!(msg[0].flags & I2C_M_RD)) {
  1435. if (num == 1) /* Write only */
  1436. i2c_smbus_add_pec(&msg[0]);
  1437. else /* Write followed by read */
  1438. partial_pec = i2c_smbus_msg_pec(0, &msg[0]);
  1439. }
  1440. /* Ask for PEC if last message is a read */
  1441. if (msg[num-1].flags & I2C_M_RD)
  1442. msg[num-1].len++;
  1443. }
  1444. status = i2c_transfer(adapter, msg, num);
  1445. if (status < 0)
  1446. return status;
  1447. /* Check PEC if last message is a read */
  1448. if (i && (msg[num-1].flags & I2C_M_RD)) {
  1449. status = i2c_smbus_check_pec(partial_pec, &msg[num-1]);
  1450. if (status < 0)
  1451. return status;
  1452. }
  1453. if (read_write == I2C_SMBUS_READ)
  1454. switch(size) {
  1455. case I2C_SMBUS_BYTE:
  1456. data->byte = msgbuf0[0];
  1457. break;
  1458. case I2C_SMBUS_BYTE_DATA:
  1459. data->byte = msgbuf1[0];
  1460. break;
  1461. case I2C_SMBUS_WORD_DATA:
  1462. case I2C_SMBUS_PROC_CALL:
  1463. data->word = msgbuf1[0] | (msgbuf1[1] << 8);
  1464. break;
  1465. case I2C_SMBUS_I2C_BLOCK_DATA:
  1466. for (i = 0; i < data->block[0]; i++)
  1467. data->block[i+1] = msgbuf1[i];
  1468. break;
  1469. case I2C_SMBUS_BLOCK_DATA:
  1470. case I2C_SMBUS_BLOCK_PROC_CALL:
  1471. for (i = 0; i < msgbuf1[0] + 1; i++)
  1472. data->block[i] = msgbuf1[i];
  1473. break;
  1474. }
  1475. return 0;
  1476. }
  1477. /**
  1478. * i2c_smbus_xfer - execute SMBus protocol operations
  1479. * @adapter: Handle to I2C bus
  1480. * @addr: Address of SMBus slave on that bus
  1481. * @flags: I2C_CLIENT_* flags (usually zero or I2C_CLIENT_PEC)
  1482. * @read_write: I2C_SMBUS_READ or I2C_SMBUS_WRITE
  1483. * @command: Byte interpreted by slave, for protocols which use such bytes
  1484. * @protocol: SMBus protocol operation to execute, such as I2C_SMBUS_PROC_CALL
  1485. * @data: Data to be read or written
  1486. *
  1487. * This executes an SMBus protocol operation, and returns a negative
  1488. * errno code else zero on success.
  1489. */
  1490. s32 i2c_smbus_xfer(struct i2c_adapter * adapter, u16 addr, unsigned short flags,
  1491. char read_write, u8 command, int protocol,
  1492. union i2c_smbus_data * data)
  1493. {
  1494. s32 res;
  1495. flags &= I2C_M_TEN | I2C_CLIENT_PEC;
  1496. if (adapter->algo->smbus_xfer) {
  1497. mutex_lock(&adapter->bus_lock);
  1498. res = adapter->algo->smbus_xfer(adapter,addr,flags,read_write,
  1499. command, protocol, data);
  1500. mutex_unlock(&adapter->bus_lock);
  1501. } else
  1502. res = i2c_smbus_xfer_emulated(adapter,addr,flags,read_write,
  1503. command, protocol, data);
  1504. return res;
  1505. }
  1506. EXPORT_SYMBOL(i2c_smbus_xfer);
  1507. MODULE_AUTHOR("Simon G. Vogl <simon@tk.uni-linz.ac.at>");
  1508. MODULE_DESCRIPTION("I2C-Bus main module");
  1509. MODULE_LICENSE("GPL");