i2c-core.c 41 KB

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