i2c-core.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215
  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> */
  19. #include <linux/module.h>
  20. #include <linux/kernel.h>
  21. #include <linux/errno.h>
  22. #include <linux/slab.h>
  23. #include <linux/i2c.h>
  24. #include <linux/init.h>
  25. #include <linux/idr.h>
  26. #include <linux/seq_file.h>
  27. #include <asm/uaccess.h>
  28. static LIST_HEAD(adapters);
  29. static LIST_HEAD(drivers);
  30. static DECLARE_MUTEX(core_lists);
  31. static DEFINE_IDR(i2c_adapter_idr);
  32. /* match always succeeds, as we want the probe() to tell if we really accept this match */
  33. static int i2c_device_match(struct device *dev, struct device_driver *drv)
  34. {
  35. return 1;
  36. }
  37. static int i2c_bus_suspend(struct device * dev, pm_message_t state)
  38. {
  39. int rc = 0;
  40. if (dev->driver && dev->driver->suspend)
  41. rc = dev->driver->suspend(dev,state,0);
  42. return rc;
  43. }
  44. static int i2c_bus_resume(struct device * dev)
  45. {
  46. int rc = 0;
  47. if (dev->driver && dev->driver->resume)
  48. rc = dev->driver->resume(dev,0);
  49. return rc;
  50. }
  51. struct bus_type i2c_bus_type = {
  52. .name = "i2c",
  53. .match = i2c_device_match,
  54. .suspend = i2c_bus_suspend,
  55. .resume = i2c_bus_resume,
  56. };
  57. static int i2c_device_probe(struct device *dev)
  58. {
  59. return -ENODEV;
  60. }
  61. static int i2c_device_remove(struct device *dev)
  62. {
  63. return 0;
  64. }
  65. void i2c_adapter_dev_release(struct device *dev)
  66. {
  67. struct i2c_adapter *adap = dev_to_i2c_adapter(dev);
  68. complete(&adap->dev_released);
  69. }
  70. struct device_driver i2c_adapter_driver = {
  71. .name = "i2c_adapter",
  72. .bus = &i2c_bus_type,
  73. .probe = i2c_device_probe,
  74. .remove = i2c_device_remove,
  75. };
  76. static void i2c_adapter_class_dev_release(struct class_device *dev)
  77. {
  78. struct i2c_adapter *adap = class_dev_to_i2c_adapter(dev);
  79. complete(&adap->class_dev_released);
  80. }
  81. struct class i2c_adapter_class = {
  82. .name = "i2c-adapter",
  83. .release = &i2c_adapter_class_dev_release,
  84. };
  85. static ssize_t show_adapter_name(struct device *dev, struct device_attribute *attr, char *buf)
  86. {
  87. struct i2c_adapter *adap = dev_to_i2c_adapter(dev);
  88. return sprintf(buf, "%s\n", adap->name);
  89. }
  90. static DEVICE_ATTR(name, S_IRUGO, show_adapter_name, NULL);
  91. static void i2c_client_release(struct device *dev)
  92. {
  93. struct i2c_client *client = to_i2c_client(dev);
  94. complete(&client->released);
  95. }
  96. static ssize_t show_client_name(struct device *dev, struct device_attribute *attr, char *buf)
  97. {
  98. struct i2c_client *client = to_i2c_client(dev);
  99. return sprintf(buf, "%s\n", client->name);
  100. }
  101. /*
  102. * We can't use the DEVICE_ATTR() macro here as we want the same filename for a
  103. * different type of a device. So beware if the DEVICE_ATTR() macro ever
  104. * changes, this definition will also have to change.
  105. */
  106. static struct device_attribute dev_attr_client_name = {
  107. .attr = {.name = "name", .mode = S_IRUGO, .owner = THIS_MODULE },
  108. .show = &show_client_name,
  109. };
  110. /* ---------------------------------------------------
  111. * registering functions
  112. * ---------------------------------------------------
  113. */
  114. /* -----
  115. * i2c_add_adapter is called from within the algorithm layer,
  116. * when a new hw adapter registers. A new device is register to be
  117. * available for clients.
  118. */
  119. int i2c_add_adapter(struct i2c_adapter *adap)
  120. {
  121. int id, res = 0;
  122. struct list_head *item;
  123. struct i2c_driver *driver;
  124. down(&core_lists);
  125. if (idr_pre_get(&i2c_adapter_idr, GFP_KERNEL) == 0) {
  126. res = -ENOMEM;
  127. goto out_unlock;
  128. }
  129. res = idr_get_new(&i2c_adapter_idr, adap, &id);
  130. if (res < 0) {
  131. if (res == -EAGAIN)
  132. res = -ENOMEM;
  133. goto out_unlock;
  134. }
  135. adap->nr = id & MAX_ID_MASK;
  136. init_MUTEX(&adap->bus_lock);
  137. init_MUTEX(&adap->clist_lock);
  138. list_add_tail(&adap->list,&adapters);
  139. INIT_LIST_HEAD(&adap->clients);
  140. /* Add the adapter to the driver core.
  141. * If the parent pointer is not set up,
  142. * we add this adapter to the host bus.
  143. */
  144. if (adap->dev.parent == NULL)
  145. adap->dev.parent = &platform_bus;
  146. sprintf(adap->dev.bus_id, "i2c-%d", adap->nr);
  147. adap->dev.driver = &i2c_adapter_driver;
  148. adap->dev.release = &i2c_adapter_dev_release;
  149. device_register(&adap->dev);
  150. device_create_file(&adap->dev, &dev_attr_name);
  151. /* Add this adapter to the i2c_adapter class */
  152. memset(&adap->class_dev, 0x00, sizeof(struct class_device));
  153. adap->class_dev.dev = &adap->dev;
  154. adap->class_dev.class = &i2c_adapter_class;
  155. strlcpy(adap->class_dev.class_id, adap->dev.bus_id, BUS_ID_SIZE);
  156. class_device_register(&adap->class_dev);
  157. dev_dbg(&adap->dev, "adapter [%s] registered\n", adap->name);
  158. /* inform drivers of new adapters */
  159. list_for_each(item,&drivers) {
  160. driver = list_entry(item, struct i2c_driver, list);
  161. if (driver->flags & I2C_DF_NOTIFY)
  162. /* We ignore the return code; if it fails, too bad */
  163. driver->attach_adapter(adap);
  164. }
  165. out_unlock:
  166. up(&core_lists);
  167. return res;
  168. }
  169. int i2c_del_adapter(struct i2c_adapter *adap)
  170. {
  171. struct list_head *item, *_n;
  172. struct i2c_adapter *adap_from_list;
  173. struct i2c_driver *driver;
  174. struct i2c_client *client;
  175. int res = 0;
  176. down(&core_lists);
  177. /* First make sure that this adapter was ever added */
  178. list_for_each_entry(adap_from_list, &adapters, list) {
  179. if (adap_from_list == adap)
  180. break;
  181. }
  182. if (adap_from_list != adap) {
  183. pr_debug("i2c-core: attempting to delete unregistered "
  184. "adapter [%s]\n", adap->name);
  185. res = -EINVAL;
  186. goto out_unlock;
  187. }
  188. list_for_each(item,&drivers) {
  189. driver = list_entry(item, struct i2c_driver, list);
  190. if (driver->detach_adapter)
  191. if ((res = driver->detach_adapter(adap))) {
  192. dev_err(&adap->dev, "detach_adapter failed "
  193. "for driver [%s]\n", driver->name);
  194. goto out_unlock;
  195. }
  196. }
  197. /* detach any active clients. This must be done first, because
  198. * it can fail; in which case we give up. */
  199. list_for_each_safe(item, _n, &adap->clients) {
  200. client = list_entry(item, struct i2c_client, list);
  201. /* detaching devices is unconditional of the set notify
  202. * flag, as _all_ clients that reside on the adapter
  203. * must be deleted, as this would cause invalid states.
  204. */
  205. if ((res=client->driver->detach_client(client))) {
  206. dev_err(&adap->dev, "detach_client failed for client "
  207. "[%s] at address 0x%02x\n", client->name,
  208. client->addr);
  209. goto out_unlock;
  210. }
  211. }
  212. /* clean up the sysfs representation */
  213. init_completion(&adap->dev_released);
  214. init_completion(&adap->class_dev_released);
  215. class_device_unregister(&adap->class_dev);
  216. device_remove_file(&adap->dev, &dev_attr_name);
  217. device_unregister(&adap->dev);
  218. list_del(&adap->list);
  219. /* wait for sysfs to drop all references */
  220. wait_for_completion(&adap->dev_released);
  221. wait_for_completion(&adap->class_dev_released);
  222. /* free dynamically allocated bus id */
  223. idr_remove(&i2c_adapter_idr, adap->nr);
  224. dev_dbg(&adap->dev, "adapter [%s] unregistered\n", adap->name);
  225. out_unlock:
  226. up(&core_lists);
  227. return res;
  228. }
  229. /* -----
  230. * What follows is the "upwards" interface: commands for talking to clients,
  231. * which implement the functions to access the physical information of the
  232. * chips.
  233. */
  234. int i2c_add_driver(struct i2c_driver *driver)
  235. {
  236. struct list_head *item;
  237. struct i2c_adapter *adapter;
  238. int res = 0;
  239. down(&core_lists);
  240. /* add the driver to the list of i2c drivers in the driver core */
  241. driver->driver.name = driver->name;
  242. driver->driver.bus = &i2c_bus_type;
  243. driver->driver.probe = i2c_device_probe;
  244. driver->driver.remove = i2c_device_remove;
  245. res = driver_register(&driver->driver);
  246. if (res)
  247. goto out_unlock;
  248. list_add_tail(&driver->list,&drivers);
  249. pr_debug("i2c-core: driver [%s] registered\n", driver->name);
  250. /* now look for instances of driver on our adapters */
  251. if (driver->flags & I2C_DF_NOTIFY) {
  252. list_for_each(item,&adapters) {
  253. adapter = list_entry(item, struct i2c_adapter, list);
  254. driver->attach_adapter(adapter);
  255. }
  256. }
  257. out_unlock:
  258. up(&core_lists);
  259. return res;
  260. }
  261. int i2c_del_driver(struct i2c_driver *driver)
  262. {
  263. struct list_head *item1, *item2, *_n;
  264. struct i2c_client *client;
  265. struct i2c_adapter *adap;
  266. int res = 0;
  267. down(&core_lists);
  268. /* Have a look at each adapter, if clients of this driver are still
  269. * attached. If so, detach them to be able to kill the driver
  270. * afterwards.
  271. *
  272. * Removing clients does not depend on the notify flag, else
  273. * invalid operation might (will!) result, when using stale client
  274. * pointers.
  275. */
  276. list_for_each(item1,&adapters) {
  277. adap = list_entry(item1, struct i2c_adapter, list);
  278. if (driver->detach_adapter) {
  279. if ((res = driver->detach_adapter(adap))) {
  280. dev_err(&adap->dev, "detach_adapter failed "
  281. "for driver [%s]\n", driver->name);
  282. goto out_unlock;
  283. }
  284. } else {
  285. list_for_each_safe(item2, _n, &adap->clients) {
  286. client = list_entry(item2, struct i2c_client, list);
  287. if (client->driver != driver)
  288. continue;
  289. dev_dbg(&adap->dev, "detaching client [%s] "
  290. "at 0x%02x\n", client->name,
  291. client->addr);
  292. if ((res = driver->detach_client(client))) {
  293. dev_err(&adap->dev, "detach_client "
  294. "failed for client [%s] at "
  295. "0x%02x\n", client->name,
  296. client->addr);
  297. goto out_unlock;
  298. }
  299. }
  300. }
  301. }
  302. driver_unregister(&driver->driver);
  303. list_del(&driver->list);
  304. pr_debug("i2c-core: driver [%s] unregistered\n", driver->name);
  305. out_unlock:
  306. up(&core_lists);
  307. return 0;
  308. }
  309. static int __i2c_check_addr(struct i2c_adapter *adapter, unsigned int addr)
  310. {
  311. struct list_head *item;
  312. struct i2c_client *client;
  313. list_for_each(item,&adapter->clients) {
  314. client = list_entry(item, struct i2c_client, list);
  315. if (client->addr == addr)
  316. return -EBUSY;
  317. }
  318. return 0;
  319. }
  320. int i2c_check_addr(struct i2c_adapter *adapter, int addr)
  321. {
  322. int rval;
  323. down(&adapter->clist_lock);
  324. rval = __i2c_check_addr(adapter, addr);
  325. up(&adapter->clist_lock);
  326. return rval;
  327. }
  328. int i2c_attach_client(struct i2c_client *client)
  329. {
  330. struct i2c_adapter *adapter = client->adapter;
  331. down(&adapter->clist_lock);
  332. if (__i2c_check_addr(client->adapter, client->addr)) {
  333. up(&adapter->clist_lock);
  334. return -EBUSY;
  335. }
  336. list_add_tail(&client->list,&adapter->clients);
  337. up(&adapter->clist_lock);
  338. if (adapter->client_register) {
  339. if (adapter->client_register(client)) {
  340. dev_dbg(&adapter->dev, "client_register "
  341. "failed for client [%s] at 0x%02x\n",
  342. client->name, client->addr);
  343. }
  344. }
  345. if (client->flags & I2C_CLIENT_ALLOW_USE)
  346. client->usage_count = 0;
  347. client->dev.parent = &client->adapter->dev;
  348. client->dev.driver = &client->driver->driver;
  349. client->dev.bus = &i2c_bus_type;
  350. client->dev.release = &i2c_client_release;
  351. snprintf(&client->dev.bus_id[0], sizeof(client->dev.bus_id),
  352. "%d-%04x", i2c_adapter_id(adapter), client->addr);
  353. dev_dbg(&adapter->dev, "client [%s] registered with bus id %s\n",
  354. client->name, client->dev.bus_id);
  355. device_register(&client->dev);
  356. device_create_file(&client->dev, &dev_attr_client_name);
  357. return 0;
  358. }
  359. int i2c_detach_client(struct i2c_client *client)
  360. {
  361. struct i2c_adapter *adapter = client->adapter;
  362. int res = 0;
  363. if ((client->flags & I2C_CLIENT_ALLOW_USE)
  364. && (client->usage_count > 0)) {
  365. dev_warn(&client->dev, "Client [%s] still busy, "
  366. "can't detach\n", client->name);
  367. return -EBUSY;
  368. }
  369. if (adapter->client_unregister) {
  370. res = adapter->client_unregister(client);
  371. if (res) {
  372. dev_err(&client->dev,
  373. "client_unregister [%s] failed, "
  374. "client not detached\n", client->name);
  375. goto out;
  376. }
  377. }
  378. down(&adapter->clist_lock);
  379. list_del(&client->list);
  380. init_completion(&client->released);
  381. device_remove_file(&client->dev, &dev_attr_client_name);
  382. device_unregister(&client->dev);
  383. up(&adapter->clist_lock);
  384. wait_for_completion(&client->released);
  385. out:
  386. return res;
  387. }
  388. static int i2c_inc_use_client(struct i2c_client *client)
  389. {
  390. if (!try_module_get(client->driver->owner))
  391. return -ENODEV;
  392. if (!try_module_get(client->adapter->owner)) {
  393. module_put(client->driver->owner);
  394. return -ENODEV;
  395. }
  396. return 0;
  397. }
  398. static void i2c_dec_use_client(struct i2c_client *client)
  399. {
  400. module_put(client->driver->owner);
  401. module_put(client->adapter->owner);
  402. }
  403. int i2c_use_client(struct i2c_client *client)
  404. {
  405. int ret;
  406. ret = i2c_inc_use_client(client);
  407. if (ret)
  408. return ret;
  409. if (client->flags & I2C_CLIENT_ALLOW_USE) {
  410. if (client->flags & I2C_CLIENT_ALLOW_MULTIPLE_USE)
  411. client->usage_count++;
  412. else if (client->usage_count > 0)
  413. goto busy;
  414. else
  415. client->usage_count++;
  416. }
  417. return 0;
  418. busy:
  419. i2c_dec_use_client(client);
  420. return -EBUSY;
  421. }
  422. int i2c_release_client(struct i2c_client *client)
  423. {
  424. if(client->flags & I2C_CLIENT_ALLOW_USE) {
  425. if(client->usage_count>0)
  426. client->usage_count--;
  427. else {
  428. pr_debug("i2c-core: %s used one too many times\n",
  429. __FUNCTION__);
  430. return -EPERM;
  431. }
  432. }
  433. i2c_dec_use_client(client);
  434. return 0;
  435. }
  436. void i2c_clients_command(struct i2c_adapter *adap, unsigned int cmd, void *arg)
  437. {
  438. struct list_head *item;
  439. struct i2c_client *client;
  440. down(&adap->clist_lock);
  441. list_for_each(item,&adap->clients) {
  442. client = list_entry(item, struct i2c_client, list);
  443. if (!try_module_get(client->driver->owner))
  444. continue;
  445. if (NULL != client->driver->command) {
  446. up(&adap->clist_lock);
  447. client->driver->command(client,cmd,arg);
  448. down(&adap->clist_lock);
  449. }
  450. module_put(client->driver->owner);
  451. }
  452. up(&adap->clist_lock);
  453. }
  454. static int __init i2c_init(void)
  455. {
  456. int retval;
  457. retval = bus_register(&i2c_bus_type);
  458. if (retval)
  459. return retval;
  460. retval = driver_register(&i2c_adapter_driver);
  461. if (retval)
  462. return retval;
  463. return class_register(&i2c_adapter_class);
  464. }
  465. static void __exit i2c_exit(void)
  466. {
  467. class_unregister(&i2c_adapter_class);
  468. driver_unregister(&i2c_adapter_driver);
  469. bus_unregister(&i2c_bus_type);
  470. }
  471. subsys_initcall(i2c_init);
  472. module_exit(i2c_exit);
  473. /* ----------------------------------------------------
  474. * the functional interface to the i2c busses.
  475. * ----------------------------------------------------
  476. */
  477. int i2c_transfer(struct i2c_adapter * adap, struct i2c_msg *msgs, int num)
  478. {
  479. int ret;
  480. if (adap->algo->master_xfer) {
  481. #ifdef DEBUG
  482. for (ret = 0; ret < num; ret++) {
  483. dev_dbg(&adap->dev, "master_xfer[%d] %c, addr=0x%02x, "
  484. "len=%d\n", ret, msgs[ret].flags & I2C_M_RD ?
  485. 'R' : 'W', msgs[ret].addr, msgs[ret].len);
  486. }
  487. #endif
  488. down(&adap->bus_lock);
  489. ret = adap->algo->master_xfer(adap,msgs,num);
  490. up(&adap->bus_lock);
  491. return ret;
  492. } else {
  493. dev_dbg(&adap->dev, "I2C level transfers not supported\n");
  494. return -ENOSYS;
  495. }
  496. }
  497. int i2c_master_send(struct i2c_client *client,const char *buf ,int count)
  498. {
  499. int ret;
  500. struct i2c_adapter *adap=client->adapter;
  501. struct i2c_msg msg;
  502. msg.addr = client->addr;
  503. msg.flags = client->flags & I2C_M_TEN;
  504. msg.len = count;
  505. msg.buf = (char *)buf;
  506. ret = i2c_transfer(adap, &msg, 1);
  507. /* If everything went ok (i.e. 1 msg transmitted), return #bytes
  508. transmitted, else error code. */
  509. return (ret == 1) ? count : ret;
  510. }
  511. int i2c_master_recv(struct i2c_client *client, char *buf ,int count)
  512. {
  513. struct i2c_adapter *adap=client->adapter;
  514. struct i2c_msg msg;
  515. int ret;
  516. msg.addr = client->addr;
  517. msg.flags = client->flags & I2C_M_TEN;
  518. msg.flags |= I2C_M_RD;
  519. msg.len = count;
  520. msg.buf = buf;
  521. ret = i2c_transfer(adap, &msg, 1);
  522. /* If everything went ok (i.e. 1 msg transmitted), return #bytes
  523. transmitted, else error code. */
  524. return (ret == 1) ? count : ret;
  525. }
  526. int i2c_control(struct i2c_client *client,
  527. unsigned int cmd, unsigned long arg)
  528. {
  529. int ret = 0;
  530. struct i2c_adapter *adap = client->adapter;
  531. dev_dbg(&client->adapter->dev, "i2c ioctl, cmd: 0x%x, arg: %#lx\n", cmd, arg);
  532. switch (cmd) {
  533. case I2C_RETRIES:
  534. adap->retries = arg;
  535. break;
  536. case I2C_TIMEOUT:
  537. adap->timeout = arg;
  538. break;
  539. default:
  540. if (adap->algo->algo_control!=NULL)
  541. ret = adap->algo->algo_control(adap,cmd,arg);
  542. }
  543. return ret;
  544. }
  545. /* ----------------------------------------------------
  546. * the i2c address scanning function
  547. * Will not work for 10-bit addresses!
  548. * ----------------------------------------------------
  549. */
  550. /* Return: kind (>= 0) if force found, -1 if not found */
  551. static inline int i2c_probe_forces(struct i2c_adapter *adapter, int addr,
  552. unsigned short **forces)
  553. {
  554. unsigned short kind;
  555. int j, adap_id = i2c_adapter_id(adapter);
  556. for (kind = 0; forces[kind]; kind++) {
  557. for (j = 0; forces[kind][j] != I2C_CLIENT_END; j += 2) {
  558. if ((forces[kind][j] == adap_id ||
  559. forces[kind][j] == ANY_I2C_BUS)
  560. && forces[kind][j + 1] == addr) {
  561. dev_dbg(&adapter->dev, "found force parameter, "
  562. "addr 0x%02x, kind %u\n", addr, kind);
  563. return kind;
  564. }
  565. }
  566. }
  567. return -1;
  568. }
  569. int i2c_probe(struct i2c_adapter *adapter,
  570. struct i2c_client_address_data *address_data,
  571. int (*found_proc) (struct i2c_adapter *, int, int))
  572. {
  573. int addr,i,found,err;
  574. int adap_id = i2c_adapter_id(adapter);
  575. /* Forget it if we can't probe using SMBUS_QUICK */
  576. if (! i2c_check_functionality(adapter,I2C_FUNC_SMBUS_QUICK))
  577. return -1;
  578. for (addr = 0x00; addr <= 0x7f; addr++) {
  579. /* Skip if already in use */
  580. if (i2c_check_addr(adapter,addr))
  581. continue;
  582. /* If it is in one of the force entries, we don't do any detection
  583. at all */
  584. found = 0;
  585. if (address_data->forces) {
  586. int kind = i2c_probe_forces(adapter, addr,
  587. address_data->forces);
  588. if (kind >= 0) { /* force found */
  589. if ((err = found_proc(adapter, addr, kind)))
  590. return err;
  591. continue;
  592. }
  593. }
  594. /* If this address is in one of the ignores, we can forget about
  595. it right now */
  596. for (i = 0;
  597. !found && (address_data->ignore[i] != I2C_CLIENT_END);
  598. i += 2) {
  599. if (((adap_id == address_data->ignore[i]) ||
  600. ((address_data->ignore[i] == ANY_I2C_BUS))) &&
  601. (addr == address_data->ignore[i+1])) {
  602. dev_dbg(&adapter->dev, "found ignore parameter for adapter %d, "
  603. "addr %04x\n", adap_id ,addr);
  604. found = 1;
  605. }
  606. }
  607. if (found)
  608. continue;
  609. /* Now, we will do a detection, but only if it is in the normal or
  610. probe entries */
  611. for (i = 0;
  612. !found && (address_data->normal_i2c[i] != I2C_CLIENT_END);
  613. i += 1) {
  614. if (addr == address_data->normal_i2c[i]) {
  615. found = 1;
  616. dev_dbg(&adapter->dev, "found normal i2c entry for adapter %d, "
  617. "addr %02x\n", adap_id, addr);
  618. }
  619. }
  620. for (i = 0;
  621. !found && (address_data->probe[i] != I2C_CLIENT_END);
  622. i += 2) {
  623. if (((adap_id == address_data->probe[i]) ||
  624. ((address_data->probe[i] == ANY_I2C_BUS))) &&
  625. (addr == address_data->probe[i+1])) {
  626. found = 1;
  627. dev_dbg(&adapter->dev, "found probe parameter for adapter %d, "
  628. "addr %04x\n", adap_id,addr);
  629. }
  630. }
  631. if (!found)
  632. continue;
  633. /* OK, so we really should examine this address. First check
  634. whether there is some client here at all! */
  635. if (i2c_smbus_xfer(adapter,addr,0,0,0,I2C_SMBUS_QUICK,NULL) >= 0)
  636. if ((err = found_proc(adapter,addr,-1)))
  637. return err;
  638. }
  639. return 0;
  640. }
  641. struct i2c_adapter* i2c_get_adapter(int id)
  642. {
  643. struct i2c_adapter *adapter;
  644. down(&core_lists);
  645. adapter = (struct i2c_adapter *)idr_find(&i2c_adapter_idr, id);
  646. if (adapter && !try_module_get(adapter->owner))
  647. adapter = NULL;
  648. up(&core_lists);
  649. return adapter;
  650. }
  651. void i2c_put_adapter(struct i2c_adapter *adap)
  652. {
  653. module_put(adap->owner);
  654. }
  655. /* The SMBus parts */
  656. #define POLY (0x1070U << 3)
  657. static u8
  658. crc8(u16 data)
  659. {
  660. int i;
  661. for(i = 0; i < 8; i++) {
  662. if (data & 0x8000)
  663. data = data ^ POLY;
  664. data = data << 1;
  665. }
  666. return (u8)(data >> 8);
  667. }
  668. /* CRC over count bytes in the first array plus the bytes in the rest
  669. array if it is non-null. rest[0] is the (length of rest) - 1
  670. and is included. */
  671. static u8 i2c_smbus_partial_pec(u8 crc, int count, u8 *first, u8 *rest)
  672. {
  673. int i;
  674. for(i = 0; i < count; i++)
  675. crc = crc8((crc ^ first[i]) << 8);
  676. if(rest != NULL)
  677. for(i = 0; i <= rest[0]; i++)
  678. crc = crc8((crc ^ rest[i]) << 8);
  679. return crc;
  680. }
  681. static u8 i2c_smbus_pec(int count, u8 *first, u8 *rest)
  682. {
  683. return i2c_smbus_partial_pec(0, count, first, rest);
  684. }
  685. /* Returns new "size" (transaction type)
  686. Note that we convert byte to byte_data and byte_data to word_data
  687. rather than invent new xxx_PEC transactions. */
  688. static int i2c_smbus_add_pec(u16 addr, u8 command, int size,
  689. union i2c_smbus_data *data)
  690. {
  691. u8 buf[3];
  692. buf[0] = addr << 1;
  693. buf[1] = command;
  694. switch(size) {
  695. case I2C_SMBUS_BYTE:
  696. data->byte = i2c_smbus_pec(2, buf, NULL);
  697. size = I2C_SMBUS_BYTE_DATA;
  698. break;
  699. case I2C_SMBUS_BYTE_DATA:
  700. buf[2] = data->byte;
  701. data->word = buf[2] ||
  702. (i2c_smbus_pec(3, buf, NULL) << 8);
  703. size = I2C_SMBUS_WORD_DATA;
  704. break;
  705. case I2C_SMBUS_WORD_DATA:
  706. /* unsupported */
  707. break;
  708. case I2C_SMBUS_BLOCK_DATA:
  709. data->block[data->block[0] + 1] =
  710. i2c_smbus_pec(2, buf, data->block);
  711. size = I2C_SMBUS_BLOCK_DATA_PEC;
  712. break;
  713. }
  714. return size;
  715. }
  716. static int i2c_smbus_check_pec(u16 addr, u8 command, int size, u8 partial,
  717. union i2c_smbus_data *data)
  718. {
  719. u8 buf[3], rpec, cpec;
  720. buf[1] = command;
  721. switch(size) {
  722. case I2C_SMBUS_BYTE_DATA:
  723. buf[0] = (addr << 1) | 1;
  724. cpec = i2c_smbus_pec(2, buf, NULL);
  725. rpec = data->byte;
  726. break;
  727. case I2C_SMBUS_WORD_DATA:
  728. buf[0] = (addr << 1) | 1;
  729. buf[2] = data->word & 0xff;
  730. cpec = i2c_smbus_pec(3, buf, NULL);
  731. rpec = data->word >> 8;
  732. break;
  733. case I2C_SMBUS_WORD_DATA_PEC:
  734. /* unsupported */
  735. cpec = rpec = 0;
  736. break;
  737. case I2C_SMBUS_PROC_CALL_PEC:
  738. /* unsupported */
  739. cpec = rpec = 0;
  740. break;
  741. case I2C_SMBUS_BLOCK_DATA_PEC:
  742. buf[0] = (addr << 1);
  743. buf[2] = (addr << 1) | 1;
  744. cpec = i2c_smbus_pec(3, buf, data->block);
  745. rpec = data->block[data->block[0] + 1];
  746. break;
  747. case I2C_SMBUS_BLOCK_PROC_CALL_PEC:
  748. buf[0] = (addr << 1) | 1;
  749. rpec = i2c_smbus_partial_pec(partial, 1,
  750. buf, data->block);
  751. cpec = data->block[data->block[0] + 1];
  752. break;
  753. default:
  754. cpec = rpec = 0;
  755. break;
  756. }
  757. if (rpec != cpec) {
  758. pr_debug("i2c-core: Bad PEC 0x%02x vs. 0x%02x\n",
  759. rpec, cpec);
  760. return -1;
  761. }
  762. return 0;
  763. }
  764. s32 i2c_smbus_write_quick(struct i2c_client *client, u8 value)
  765. {
  766. return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
  767. value,0,I2C_SMBUS_QUICK,NULL);
  768. }
  769. s32 i2c_smbus_read_byte(struct i2c_client *client)
  770. {
  771. union i2c_smbus_data data;
  772. if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
  773. I2C_SMBUS_READ,0,I2C_SMBUS_BYTE, &data))
  774. return -1;
  775. else
  776. return 0x0FF & data.byte;
  777. }
  778. s32 i2c_smbus_write_byte(struct i2c_client *client, u8 value)
  779. {
  780. union i2c_smbus_data data; /* only for PEC */
  781. return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
  782. I2C_SMBUS_WRITE,value, I2C_SMBUS_BYTE,&data);
  783. }
  784. s32 i2c_smbus_read_byte_data(struct i2c_client *client, u8 command)
  785. {
  786. union i2c_smbus_data data;
  787. if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
  788. I2C_SMBUS_READ,command, I2C_SMBUS_BYTE_DATA,&data))
  789. return -1;
  790. else
  791. return 0x0FF & data.byte;
  792. }
  793. s32 i2c_smbus_write_byte_data(struct i2c_client *client, u8 command, u8 value)
  794. {
  795. union i2c_smbus_data data;
  796. data.byte = value;
  797. return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
  798. I2C_SMBUS_WRITE,command,
  799. I2C_SMBUS_BYTE_DATA,&data);
  800. }
  801. s32 i2c_smbus_read_word_data(struct i2c_client *client, u8 command)
  802. {
  803. union i2c_smbus_data data;
  804. if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
  805. I2C_SMBUS_READ,command, I2C_SMBUS_WORD_DATA, &data))
  806. return -1;
  807. else
  808. return 0x0FFFF & data.word;
  809. }
  810. s32 i2c_smbus_write_word_data(struct i2c_client *client, u8 command, u16 value)
  811. {
  812. union i2c_smbus_data data;
  813. data.word = value;
  814. return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
  815. I2C_SMBUS_WRITE,command,
  816. I2C_SMBUS_WORD_DATA,&data);
  817. }
  818. s32 i2c_smbus_write_block_data(struct i2c_client *client, u8 command,
  819. u8 length, u8 *values)
  820. {
  821. union i2c_smbus_data data;
  822. int i;
  823. if (length > I2C_SMBUS_BLOCK_MAX)
  824. length = I2C_SMBUS_BLOCK_MAX;
  825. for (i = 1; i <= length; i++)
  826. data.block[i] = values[i-1];
  827. data.block[0] = length;
  828. return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
  829. I2C_SMBUS_WRITE,command,
  830. I2C_SMBUS_BLOCK_DATA,&data);
  831. }
  832. /* Returns the number of read bytes */
  833. s32 i2c_smbus_read_i2c_block_data(struct i2c_client *client, u8 command, u8 *values)
  834. {
  835. union i2c_smbus_data data;
  836. int i;
  837. if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
  838. I2C_SMBUS_READ,command,
  839. I2C_SMBUS_I2C_BLOCK_DATA,&data))
  840. return -1;
  841. else {
  842. for (i = 1; i <= data.block[0]; i++)
  843. values[i-1] = data.block[i];
  844. return data.block[0];
  845. }
  846. }
  847. /* Simulate a SMBus command using the i2c protocol
  848. No checking of parameters is done! */
  849. static s32 i2c_smbus_xfer_emulated(struct i2c_adapter * adapter, u16 addr,
  850. unsigned short flags,
  851. char read_write, u8 command, int size,
  852. union i2c_smbus_data * data)
  853. {
  854. /* So we need to generate a series of msgs. In the case of writing, we
  855. need to use only one message; when reading, we need two. We initialize
  856. most things with sane defaults, to keep the code below somewhat
  857. simpler. */
  858. unsigned char msgbuf0[34];
  859. unsigned char msgbuf1[34];
  860. int num = read_write == I2C_SMBUS_READ?2:1;
  861. struct i2c_msg msg[2] = { { addr, flags, 1, msgbuf0 },
  862. { addr, flags | I2C_M_RD, 0, msgbuf1 }
  863. };
  864. int i;
  865. msgbuf0[0] = command;
  866. switch(size) {
  867. case I2C_SMBUS_QUICK:
  868. msg[0].len = 0;
  869. /* Special case: The read/write field is used as data */
  870. msg[0].flags = flags | (read_write==I2C_SMBUS_READ)?I2C_M_RD:0;
  871. num = 1;
  872. break;
  873. case I2C_SMBUS_BYTE:
  874. if (read_write == I2C_SMBUS_READ) {
  875. /* Special case: only a read! */
  876. msg[0].flags = I2C_M_RD | flags;
  877. num = 1;
  878. }
  879. break;
  880. case I2C_SMBUS_BYTE_DATA:
  881. if (read_write == I2C_SMBUS_READ)
  882. msg[1].len = 1;
  883. else {
  884. msg[0].len = 2;
  885. msgbuf0[1] = data->byte;
  886. }
  887. break;
  888. case I2C_SMBUS_WORD_DATA:
  889. if (read_write == I2C_SMBUS_READ)
  890. msg[1].len = 2;
  891. else {
  892. msg[0].len=3;
  893. msgbuf0[1] = data->word & 0xff;
  894. msgbuf0[2] = (data->word >> 8) & 0xff;
  895. }
  896. break;
  897. case I2C_SMBUS_PROC_CALL:
  898. num = 2; /* Special case */
  899. read_write = I2C_SMBUS_READ;
  900. msg[0].len = 3;
  901. msg[1].len = 2;
  902. msgbuf0[1] = data->word & 0xff;
  903. msgbuf0[2] = (data->word >> 8) & 0xff;
  904. break;
  905. case I2C_SMBUS_BLOCK_DATA:
  906. case I2C_SMBUS_BLOCK_DATA_PEC:
  907. if (read_write == I2C_SMBUS_READ) {
  908. dev_err(&adapter->dev, "Block read not supported "
  909. "under I2C emulation!\n");
  910. return -1;
  911. } else {
  912. msg[0].len = data->block[0] + 2;
  913. if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 2) {
  914. dev_err(&adapter->dev, "smbus_access called with "
  915. "invalid block write size (%d)\n",
  916. data->block[0]);
  917. return -1;
  918. }
  919. if(size == I2C_SMBUS_BLOCK_DATA_PEC)
  920. (msg[0].len)++;
  921. for (i = 1; i <= msg[0].len; i++)
  922. msgbuf0[i] = data->block[i-1];
  923. }
  924. break;
  925. case I2C_SMBUS_BLOCK_PROC_CALL:
  926. case I2C_SMBUS_BLOCK_PROC_CALL_PEC:
  927. dev_dbg(&adapter->dev, "Block process call not supported "
  928. "under I2C emulation!\n");
  929. return -1;
  930. case I2C_SMBUS_I2C_BLOCK_DATA:
  931. if (read_write == I2C_SMBUS_READ) {
  932. msg[1].len = I2C_SMBUS_I2C_BLOCK_MAX;
  933. } else {
  934. msg[0].len = data->block[0] + 1;
  935. if (msg[0].len > I2C_SMBUS_I2C_BLOCK_MAX + 1) {
  936. dev_err(&adapter->dev, "i2c_smbus_xfer_emulated called with "
  937. "invalid block write size (%d)\n",
  938. data->block[0]);
  939. return -1;
  940. }
  941. for (i = 1; i <= data->block[0]; i++)
  942. msgbuf0[i] = data->block[i];
  943. }
  944. break;
  945. default:
  946. dev_err(&adapter->dev, "smbus_access called with invalid size (%d)\n",
  947. size);
  948. return -1;
  949. }
  950. if (i2c_transfer(adapter, msg, num) < 0)
  951. return -1;
  952. if (read_write == I2C_SMBUS_READ)
  953. switch(size) {
  954. case I2C_SMBUS_BYTE:
  955. data->byte = msgbuf0[0];
  956. break;
  957. case I2C_SMBUS_BYTE_DATA:
  958. data->byte = msgbuf1[0];
  959. break;
  960. case I2C_SMBUS_WORD_DATA:
  961. case I2C_SMBUS_PROC_CALL:
  962. data->word = msgbuf1[0] | (msgbuf1[1] << 8);
  963. break;
  964. case I2C_SMBUS_I2C_BLOCK_DATA:
  965. /* fixed at 32 for now */
  966. data->block[0] = I2C_SMBUS_I2C_BLOCK_MAX;
  967. for (i = 0; i < I2C_SMBUS_I2C_BLOCK_MAX; i++)
  968. data->block[i+1] = msgbuf1[i];
  969. break;
  970. }
  971. return 0;
  972. }
  973. s32 i2c_smbus_xfer(struct i2c_adapter * adapter, u16 addr, unsigned short flags,
  974. char read_write, u8 command, int size,
  975. union i2c_smbus_data * data)
  976. {
  977. s32 res;
  978. int swpec = 0;
  979. u8 partial = 0;
  980. flags &= I2C_M_TEN | I2C_CLIENT_PEC;
  981. if((flags & I2C_CLIENT_PEC) &&
  982. !(i2c_check_functionality(adapter, I2C_FUNC_SMBUS_HWPEC_CALC))) {
  983. swpec = 1;
  984. if(read_write == I2C_SMBUS_READ &&
  985. size == I2C_SMBUS_BLOCK_DATA)
  986. size = I2C_SMBUS_BLOCK_DATA_PEC;
  987. else if(size == I2C_SMBUS_PROC_CALL)
  988. size = I2C_SMBUS_PROC_CALL_PEC;
  989. else if(size == I2C_SMBUS_BLOCK_PROC_CALL) {
  990. i2c_smbus_add_pec(addr, command,
  991. I2C_SMBUS_BLOCK_DATA, data);
  992. partial = data->block[data->block[0] + 1];
  993. size = I2C_SMBUS_BLOCK_PROC_CALL_PEC;
  994. } else if(read_write == I2C_SMBUS_WRITE &&
  995. size != I2C_SMBUS_QUICK &&
  996. size != I2C_SMBUS_I2C_BLOCK_DATA)
  997. size = i2c_smbus_add_pec(addr, command, size, data);
  998. }
  999. if (adapter->algo->smbus_xfer) {
  1000. down(&adapter->bus_lock);
  1001. res = adapter->algo->smbus_xfer(adapter,addr,flags,read_write,
  1002. command,size,data);
  1003. up(&adapter->bus_lock);
  1004. } else
  1005. res = i2c_smbus_xfer_emulated(adapter,addr,flags,read_write,
  1006. command,size,data);
  1007. if(res >= 0 && swpec &&
  1008. size != I2C_SMBUS_QUICK && size != I2C_SMBUS_I2C_BLOCK_DATA &&
  1009. (read_write == I2C_SMBUS_READ || size == I2C_SMBUS_PROC_CALL_PEC ||
  1010. size == I2C_SMBUS_BLOCK_PROC_CALL_PEC)) {
  1011. if(i2c_smbus_check_pec(addr, command, size, partial, data))
  1012. return -1;
  1013. }
  1014. return res;
  1015. }
  1016. /* Next four are needed by i2c-isa */
  1017. EXPORT_SYMBOL_GPL(i2c_adapter_dev_release);
  1018. EXPORT_SYMBOL_GPL(i2c_adapter_driver);
  1019. EXPORT_SYMBOL_GPL(i2c_adapter_class);
  1020. EXPORT_SYMBOL_GPL(i2c_bus_type);
  1021. EXPORT_SYMBOL(i2c_add_adapter);
  1022. EXPORT_SYMBOL(i2c_del_adapter);
  1023. EXPORT_SYMBOL(i2c_add_driver);
  1024. EXPORT_SYMBOL(i2c_del_driver);
  1025. EXPORT_SYMBOL(i2c_attach_client);
  1026. EXPORT_SYMBOL(i2c_detach_client);
  1027. EXPORT_SYMBOL(i2c_use_client);
  1028. EXPORT_SYMBOL(i2c_release_client);
  1029. EXPORT_SYMBOL(i2c_clients_command);
  1030. EXPORT_SYMBOL(i2c_check_addr);
  1031. EXPORT_SYMBOL(i2c_master_send);
  1032. EXPORT_SYMBOL(i2c_master_recv);
  1033. EXPORT_SYMBOL(i2c_control);
  1034. EXPORT_SYMBOL(i2c_transfer);
  1035. EXPORT_SYMBOL(i2c_get_adapter);
  1036. EXPORT_SYMBOL(i2c_put_adapter);
  1037. EXPORT_SYMBOL(i2c_probe);
  1038. EXPORT_SYMBOL(i2c_smbus_xfer);
  1039. EXPORT_SYMBOL(i2c_smbus_write_quick);
  1040. EXPORT_SYMBOL(i2c_smbus_read_byte);
  1041. EXPORT_SYMBOL(i2c_smbus_write_byte);
  1042. EXPORT_SYMBOL(i2c_smbus_read_byte_data);
  1043. EXPORT_SYMBOL(i2c_smbus_write_byte_data);
  1044. EXPORT_SYMBOL(i2c_smbus_read_word_data);
  1045. EXPORT_SYMBOL(i2c_smbus_write_word_data);
  1046. EXPORT_SYMBOL(i2c_smbus_write_block_data);
  1047. EXPORT_SYMBOL(i2c_smbus_read_i2c_block_data);
  1048. MODULE_AUTHOR("Simon G. Vogl <simon@tk.uni-linz.ac.at>");
  1049. MODULE_DESCRIPTION("I2C-Bus main module");
  1050. MODULE_LICENSE("GPL");