i2c-core.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242
  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);
  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);
  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. static int i2c_probe_address(struct i2c_adapter *adapter, int addr, int kind,
  551. int (*found_proc) (struct i2c_adapter *, int, int))
  552. {
  553. int err;
  554. /* Make sure the address is valid */
  555. if (addr < 0x03 || addr > 0x77) {
  556. dev_warn(&adapter->dev, "Invalid probe address 0x%02x\n",
  557. addr);
  558. return -EINVAL;
  559. }
  560. /* Skip if already in use */
  561. if (i2c_check_addr(adapter, addr))
  562. return 0;
  563. /* Make sure there is something at this address, unless forced */
  564. if (kind < 0) {
  565. if (i2c_smbus_xfer(adapter, addr, 0, 0, 0,
  566. I2C_SMBUS_QUICK, NULL) < 0)
  567. return 0;
  568. /* prevent 24RF08 corruption */
  569. if ((addr & ~0x0f) == 0x50)
  570. i2c_smbus_xfer(adapter, addr, 0, 0, 0,
  571. I2C_SMBUS_QUICK, NULL);
  572. }
  573. /* Finally call the custom detection function */
  574. err = found_proc(adapter, addr, kind);
  575. /* -ENODEV can be returned if there is a chip at the given address
  576. but it isn't supported by this chip driver. We catch it here as
  577. this isn't an error. */
  578. return (err == -ENODEV) ? 0 : err;
  579. }
  580. int i2c_probe(struct i2c_adapter *adapter,
  581. struct i2c_client_address_data *address_data,
  582. int (*found_proc) (struct i2c_adapter *, int, int))
  583. {
  584. int i, err;
  585. int adap_id = i2c_adapter_id(adapter);
  586. /* Force entries are done first, and are not affected by ignore
  587. entries */
  588. if (address_data->forces) {
  589. unsigned short **forces = address_data->forces;
  590. int kind;
  591. for (kind = 0; forces[kind]; kind++) {
  592. for (i = 0; forces[kind][i] != I2C_CLIENT_END;
  593. i += 2) {
  594. if (forces[kind][i] == adap_id
  595. || forces[kind][i] == ANY_I2C_BUS) {
  596. dev_dbg(&adapter->dev, "found force "
  597. "parameter for adapter %d, "
  598. "addr 0x%02x, kind %d\n",
  599. adap_id, forces[kind][i + 1],
  600. kind);
  601. err = i2c_probe_address(adapter,
  602. forces[kind][i + 1],
  603. kind, found_proc);
  604. if (err)
  605. return err;
  606. }
  607. }
  608. }
  609. }
  610. /* Stop here if we can't use SMBUS_QUICK */
  611. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_QUICK)) {
  612. if (address_data->probe[0] == I2C_CLIENT_END
  613. && address_data->normal_i2c[0] == I2C_CLIENT_END)
  614. return 0;
  615. dev_warn(&adapter->dev, "SMBus Quick command not supported, "
  616. "can't probe for chips\n");
  617. return -1;
  618. }
  619. /* Probe entries are done second, and are not affected by ignore
  620. entries either */
  621. for (i = 0; address_data->probe[i] != I2C_CLIENT_END; i += 2) {
  622. if (address_data->probe[i] == adap_id
  623. || address_data->probe[i] == ANY_I2C_BUS) {
  624. dev_dbg(&adapter->dev, "found probe parameter for "
  625. "adapter %d, addr 0x%02x\n", adap_id,
  626. address_data->probe[i + 1]);
  627. err = i2c_probe_address(adapter,
  628. address_data->probe[i + 1],
  629. -1, found_proc);
  630. if (err)
  631. return err;
  632. }
  633. }
  634. /* Normal entries are done last, unless shadowed by an ignore entry */
  635. for (i = 0; address_data->normal_i2c[i] != I2C_CLIENT_END; i += 1) {
  636. int j, ignore;
  637. ignore = 0;
  638. for (j = 0; address_data->ignore[j] != I2C_CLIENT_END;
  639. j += 2) {
  640. if ((address_data->ignore[j] == adap_id ||
  641. address_data->ignore[j] == ANY_I2C_BUS)
  642. && address_data->ignore[j + 1]
  643. == address_data->normal_i2c[i]) {
  644. dev_dbg(&adapter->dev, "found ignore "
  645. "parameter for adapter %d, "
  646. "addr 0x%02x\n", adap_id,
  647. address_data->ignore[j + 1]);
  648. }
  649. ignore = 1;
  650. break;
  651. }
  652. if (ignore)
  653. continue;
  654. dev_dbg(&adapter->dev, "found normal entry for adapter %d, "
  655. "addr 0x%02x\n", adap_id,
  656. address_data->normal_i2c[i]);
  657. err = i2c_probe_address(adapter, address_data->normal_i2c[i],
  658. -1, found_proc);
  659. if (err)
  660. return err;
  661. }
  662. return 0;
  663. }
  664. struct i2c_adapter* i2c_get_adapter(int id)
  665. {
  666. struct i2c_adapter *adapter;
  667. down(&core_lists);
  668. adapter = (struct i2c_adapter *)idr_find(&i2c_adapter_idr, id);
  669. if (adapter && !try_module_get(adapter->owner))
  670. adapter = NULL;
  671. up(&core_lists);
  672. return adapter;
  673. }
  674. void i2c_put_adapter(struct i2c_adapter *adap)
  675. {
  676. module_put(adap->owner);
  677. }
  678. /* The SMBus parts */
  679. #define POLY (0x1070U << 3)
  680. static u8
  681. crc8(u16 data)
  682. {
  683. int i;
  684. for(i = 0; i < 8; i++) {
  685. if (data & 0x8000)
  686. data = data ^ POLY;
  687. data = data << 1;
  688. }
  689. return (u8)(data >> 8);
  690. }
  691. /* CRC over count bytes in the first array plus the bytes in the rest
  692. array if it is non-null. rest[0] is the (length of rest) - 1
  693. and is included. */
  694. static u8 i2c_smbus_partial_pec(u8 crc, int count, u8 *first, u8 *rest)
  695. {
  696. int i;
  697. for(i = 0; i < count; i++)
  698. crc = crc8((crc ^ first[i]) << 8);
  699. if(rest != NULL)
  700. for(i = 0; i <= rest[0]; i++)
  701. crc = crc8((crc ^ rest[i]) << 8);
  702. return crc;
  703. }
  704. static u8 i2c_smbus_pec(int count, u8 *first, u8 *rest)
  705. {
  706. return i2c_smbus_partial_pec(0, count, first, rest);
  707. }
  708. /* Returns new "size" (transaction type)
  709. Note that we convert byte to byte_data and byte_data to word_data
  710. rather than invent new xxx_PEC transactions. */
  711. static int i2c_smbus_add_pec(u16 addr, u8 command, int size,
  712. union i2c_smbus_data *data)
  713. {
  714. u8 buf[3];
  715. buf[0] = addr << 1;
  716. buf[1] = command;
  717. switch(size) {
  718. case I2C_SMBUS_BYTE:
  719. data->byte = i2c_smbus_pec(2, buf, NULL);
  720. size = I2C_SMBUS_BYTE_DATA;
  721. break;
  722. case I2C_SMBUS_BYTE_DATA:
  723. buf[2] = data->byte;
  724. data->word = buf[2] ||
  725. (i2c_smbus_pec(3, buf, NULL) << 8);
  726. size = I2C_SMBUS_WORD_DATA;
  727. break;
  728. case I2C_SMBUS_WORD_DATA:
  729. /* unsupported */
  730. break;
  731. case I2C_SMBUS_BLOCK_DATA:
  732. data->block[data->block[0] + 1] =
  733. i2c_smbus_pec(2, buf, data->block);
  734. size = I2C_SMBUS_BLOCK_DATA_PEC;
  735. break;
  736. }
  737. return size;
  738. }
  739. static int i2c_smbus_check_pec(u16 addr, u8 command, int size, u8 partial,
  740. union i2c_smbus_data *data)
  741. {
  742. u8 buf[3], rpec, cpec;
  743. buf[1] = command;
  744. switch(size) {
  745. case I2C_SMBUS_BYTE_DATA:
  746. buf[0] = (addr << 1) | 1;
  747. cpec = i2c_smbus_pec(2, buf, NULL);
  748. rpec = data->byte;
  749. break;
  750. case I2C_SMBUS_WORD_DATA:
  751. buf[0] = (addr << 1) | 1;
  752. buf[2] = data->word & 0xff;
  753. cpec = i2c_smbus_pec(3, buf, NULL);
  754. rpec = data->word >> 8;
  755. break;
  756. case I2C_SMBUS_WORD_DATA_PEC:
  757. /* unsupported */
  758. cpec = rpec = 0;
  759. break;
  760. case I2C_SMBUS_PROC_CALL_PEC:
  761. /* unsupported */
  762. cpec = rpec = 0;
  763. break;
  764. case I2C_SMBUS_BLOCK_DATA_PEC:
  765. buf[0] = (addr << 1);
  766. buf[2] = (addr << 1) | 1;
  767. cpec = i2c_smbus_pec(3, buf, data->block);
  768. rpec = data->block[data->block[0] + 1];
  769. break;
  770. case I2C_SMBUS_BLOCK_PROC_CALL_PEC:
  771. buf[0] = (addr << 1) | 1;
  772. rpec = i2c_smbus_partial_pec(partial, 1,
  773. buf, data->block);
  774. cpec = data->block[data->block[0] + 1];
  775. break;
  776. default:
  777. cpec = rpec = 0;
  778. break;
  779. }
  780. if (rpec != cpec) {
  781. pr_debug("i2c-core: Bad PEC 0x%02x vs. 0x%02x\n",
  782. rpec, cpec);
  783. return -1;
  784. }
  785. return 0;
  786. }
  787. s32 i2c_smbus_write_quick(struct i2c_client *client, u8 value)
  788. {
  789. return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
  790. value,0,I2C_SMBUS_QUICK,NULL);
  791. }
  792. s32 i2c_smbus_read_byte(struct i2c_client *client)
  793. {
  794. union i2c_smbus_data data;
  795. if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
  796. I2C_SMBUS_READ,0,I2C_SMBUS_BYTE, &data))
  797. return -1;
  798. else
  799. return 0x0FF & data.byte;
  800. }
  801. s32 i2c_smbus_write_byte(struct i2c_client *client, u8 value)
  802. {
  803. union i2c_smbus_data data; /* only for PEC */
  804. return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
  805. I2C_SMBUS_WRITE,value, I2C_SMBUS_BYTE,&data);
  806. }
  807. s32 i2c_smbus_read_byte_data(struct i2c_client *client, u8 command)
  808. {
  809. union i2c_smbus_data data;
  810. if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
  811. I2C_SMBUS_READ,command, I2C_SMBUS_BYTE_DATA,&data))
  812. return -1;
  813. else
  814. return 0x0FF & data.byte;
  815. }
  816. s32 i2c_smbus_write_byte_data(struct i2c_client *client, u8 command, u8 value)
  817. {
  818. union i2c_smbus_data data;
  819. data.byte = value;
  820. return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
  821. I2C_SMBUS_WRITE,command,
  822. I2C_SMBUS_BYTE_DATA,&data);
  823. }
  824. s32 i2c_smbus_read_word_data(struct i2c_client *client, u8 command)
  825. {
  826. union i2c_smbus_data data;
  827. if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
  828. I2C_SMBUS_READ,command, I2C_SMBUS_WORD_DATA, &data))
  829. return -1;
  830. else
  831. return 0x0FFFF & data.word;
  832. }
  833. s32 i2c_smbus_write_word_data(struct i2c_client *client, u8 command, u16 value)
  834. {
  835. union i2c_smbus_data data;
  836. data.word = value;
  837. return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
  838. I2C_SMBUS_WRITE,command,
  839. I2C_SMBUS_WORD_DATA,&data);
  840. }
  841. s32 i2c_smbus_write_block_data(struct i2c_client *client, u8 command,
  842. u8 length, u8 *values)
  843. {
  844. union i2c_smbus_data data;
  845. int i;
  846. if (length > I2C_SMBUS_BLOCK_MAX)
  847. length = I2C_SMBUS_BLOCK_MAX;
  848. for (i = 1; i <= length; i++)
  849. data.block[i] = values[i-1];
  850. data.block[0] = length;
  851. return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
  852. I2C_SMBUS_WRITE,command,
  853. I2C_SMBUS_BLOCK_DATA,&data);
  854. }
  855. /* Returns the number of read bytes */
  856. s32 i2c_smbus_read_i2c_block_data(struct i2c_client *client, u8 command, u8 *values)
  857. {
  858. union i2c_smbus_data data;
  859. int i;
  860. if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
  861. I2C_SMBUS_READ,command,
  862. I2C_SMBUS_I2C_BLOCK_DATA,&data))
  863. return -1;
  864. else {
  865. for (i = 1; i <= data.block[0]; i++)
  866. values[i-1] = data.block[i];
  867. return data.block[0];
  868. }
  869. }
  870. /* Simulate a SMBus command using the i2c protocol
  871. No checking of parameters is done! */
  872. static s32 i2c_smbus_xfer_emulated(struct i2c_adapter * adapter, u16 addr,
  873. unsigned short flags,
  874. char read_write, u8 command, int size,
  875. union i2c_smbus_data * data)
  876. {
  877. /* So we need to generate a series of msgs. In the case of writing, we
  878. need to use only one message; when reading, we need two. We initialize
  879. most things with sane defaults, to keep the code below somewhat
  880. simpler. */
  881. unsigned char msgbuf0[34];
  882. unsigned char msgbuf1[34];
  883. int num = read_write == I2C_SMBUS_READ?2:1;
  884. struct i2c_msg msg[2] = { { addr, flags, 1, msgbuf0 },
  885. { addr, flags | I2C_M_RD, 0, msgbuf1 }
  886. };
  887. int i;
  888. msgbuf0[0] = command;
  889. switch(size) {
  890. case I2C_SMBUS_QUICK:
  891. msg[0].len = 0;
  892. /* Special case: The read/write field is used as data */
  893. msg[0].flags = flags | (read_write==I2C_SMBUS_READ)?I2C_M_RD:0;
  894. num = 1;
  895. break;
  896. case I2C_SMBUS_BYTE:
  897. if (read_write == I2C_SMBUS_READ) {
  898. /* Special case: only a read! */
  899. msg[0].flags = I2C_M_RD | flags;
  900. num = 1;
  901. }
  902. break;
  903. case I2C_SMBUS_BYTE_DATA:
  904. if (read_write == I2C_SMBUS_READ)
  905. msg[1].len = 1;
  906. else {
  907. msg[0].len = 2;
  908. msgbuf0[1] = data->byte;
  909. }
  910. break;
  911. case I2C_SMBUS_WORD_DATA:
  912. if (read_write == I2C_SMBUS_READ)
  913. msg[1].len = 2;
  914. else {
  915. msg[0].len=3;
  916. msgbuf0[1] = data->word & 0xff;
  917. msgbuf0[2] = (data->word >> 8) & 0xff;
  918. }
  919. break;
  920. case I2C_SMBUS_PROC_CALL:
  921. num = 2; /* Special case */
  922. read_write = I2C_SMBUS_READ;
  923. msg[0].len = 3;
  924. msg[1].len = 2;
  925. msgbuf0[1] = data->word & 0xff;
  926. msgbuf0[2] = (data->word >> 8) & 0xff;
  927. break;
  928. case I2C_SMBUS_BLOCK_DATA:
  929. case I2C_SMBUS_BLOCK_DATA_PEC:
  930. if (read_write == I2C_SMBUS_READ) {
  931. dev_err(&adapter->dev, "Block read not supported "
  932. "under I2C emulation!\n");
  933. return -1;
  934. } else {
  935. msg[0].len = data->block[0] + 2;
  936. if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 2) {
  937. dev_err(&adapter->dev, "smbus_access called with "
  938. "invalid block write size (%d)\n",
  939. data->block[0]);
  940. return -1;
  941. }
  942. if(size == I2C_SMBUS_BLOCK_DATA_PEC)
  943. (msg[0].len)++;
  944. for (i = 1; i <= msg[0].len; i++)
  945. msgbuf0[i] = data->block[i-1];
  946. }
  947. break;
  948. case I2C_SMBUS_BLOCK_PROC_CALL:
  949. case I2C_SMBUS_BLOCK_PROC_CALL_PEC:
  950. dev_dbg(&adapter->dev, "Block process call not supported "
  951. "under I2C emulation!\n");
  952. return -1;
  953. case I2C_SMBUS_I2C_BLOCK_DATA:
  954. if (read_write == I2C_SMBUS_READ) {
  955. msg[1].len = I2C_SMBUS_I2C_BLOCK_MAX;
  956. } else {
  957. msg[0].len = data->block[0] + 1;
  958. if (msg[0].len > I2C_SMBUS_I2C_BLOCK_MAX + 1) {
  959. dev_err(&adapter->dev, "i2c_smbus_xfer_emulated called with "
  960. "invalid block write size (%d)\n",
  961. data->block[0]);
  962. return -1;
  963. }
  964. for (i = 1; i <= data->block[0]; i++)
  965. msgbuf0[i] = data->block[i];
  966. }
  967. break;
  968. default:
  969. dev_err(&adapter->dev, "smbus_access called with invalid size (%d)\n",
  970. size);
  971. return -1;
  972. }
  973. if (i2c_transfer(adapter, msg, num) < 0)
  974. return -1;
  975. if (read_write == I2C_SMBUS_READ)
  976. switch(size) {
  977. case I2C_SMBUS_BYTE:
  978. data->byte = msgbuf0[0];
  979. break;
  980. case I2C_SMBUS_BYTE_DATA:
  981. data->byte = msgbuf1[0];
  982. break;
  983. case I2C_SMBUS_WORD_DATA:
  984. case I2C_SMBUS_PROC_CALL:
  985. data->word = msgbuf1[0] | (msgbuf1[1] << 8);
  986. break;
  987. case I2C_SMBUS_I2C_BLOCK_DATA:
  988. /* fixed at 32 for now */
  989. data->block[0] = I2C_SMBUS_I2C_BLOCK_MAX;
  990. for (i = 0; i < I2C_SMBUS_I2C_BLOCK_MAX; i++)
  991. data->block[i+1] = msgbuf1[i];
  992. break;
  993. }
  994. return 0;
  995. }
  996. s32 i2c_smbus_xfer(struct i2c_adapter * adapter, u16 addr, unsigned short flags,
  997. char read_write, u8 command, int size,
  998. union i2c_smbus_data * data)
  999. {
  1000. s32 res;
  1001. int swpec = 0;
  1002. u8 partial = 0;
  1003. flags &= I2C_M_TEN | I2C_CLIENT_PEC;
  1004. if((flags & I2C_CLIENT_PEC) &&
  1005. !(i2c_check_functionality(adapter, I2C_FUNC_SMBUS_HWPEC_CALC))) {
  1006. swpec = 1;
  1007. if(read_write == I2C_SMBUS_READ &&
  1008. size == I2C_SMBUS_BLOCK_DATA)
  1009. size = I2C_SMBUS_BLOCK_DATA_PEC;
  1010. else if(size == I2C_SMBUS_PROC_CALL)
  1011. size = I2C_SMBUS_PROC_CALL_PEC;
  1012. else if(size == I2C_SMBUS_BLOCK_PROC_CALL) {
  1013. i2c_smbus_add_pec(addr, command,
  1014. I2C_SMBUS_BLOCK_DATA, data);
  1015. partial = data->block[data->block[0] + 1];
  1016. size = I2C_SMBUS_BLOCK_PROC_CALL_PEC;
  1017. } else if(read_write == I2C_SMBUS_WRITE &&
  1018. size != I2C_SMBUS_QUICK &&
  1019. size != I2C_SMBUS_I2C_BLOCK_DATA)
  1020. size = i2c_smbus_add_pec(addr, command, size, data);
  1021. }
  1022. if (adapter->algo->smbus_xfer) {
  1023. down(&adapter->bus_lock);
  1024. res = adapter->algo->smbus_xfer(adapter,addr,flags,read_write,
  1025. command,size,data);
  1026. up(&adapter->bus_lock);
  1027. } else
  1028. res = i2c_smbus_xfer_emulated(adapter,addr,flags,read_write,
  1029. command,size,data);
  1030. if(res >= 0 && swpec &&
  1031. size != I2C_SMBUS_QUICK && size != I2C_SMBUS_I2C_BLOCK_DATA &&
  1032. (read_write == I2C_SMBUS_READ || size == I2C_SMBUS_PROC_CALL_PEC ||
  1033. size == I2C_SMBUS_BLOCK_PROC_CALL_PEC)) {
  1034. if(i2c_smbus_check_pec(addr, command, size, partial, data))
  1035. return -1;
  1036. }
  1037. return res;
  1038. }
  1039. /* Next four are needed by i2c-isa */
  1040. EXPORT_SYMBOL_GPL(i2c_adapter_dev_release);
  1041. EXPORT_SYMBOL_GPL(i2c_adapter_driver);
  1042. EXPORT_SYMBOL_GPL(i2c_adapter_class);
  1043. EXPORT_SYMBOL_GPL(i2c_bus_type);
  1044. EXPORT_SYMBOL(i2c_add_adapter);
  1045. EXPORT_SYMBOL(i2c_del_adapter);
  1046. EXPORT_SYMBOL(i2c_add_driver);
  1047. EXPORT_SYMBOL(i2c_del_driver);
  1048. EXPORT_SYMBOL(i2c_attach_client);
  1049. EXPORT_SYMBOL(i2c_detach_client);
  1050. EXPORT_SYMBOL(i2c_use_client);
  1051. EXPORT_SYMBOL(i2c_release_client);
  1052. EXPORT_SYMBOL(i2c_clients_command);
  1053. EXPORT_SYMBOL(i2c_check_addr);
  1054. EXPORT_SYMBOL(i2c_master_send);
  1055. EXPORT_SYMBOL(i2c_master_recv);
  1056. EXPORT_SYMBOL(i2c_control);
  1057. EXPORT_SYMBOL(i2c_transfer);
  1058. EXPORT_SYMBOL(i2c_get_adapter);
  1059. EXPORT_SYMBOL(i2c_put_adapter);
  1060. EXPORT_SYMBOL(i2c_probe);
  1061. EXPORT_SYMBOL(i2c_smbus_xfer);
  1062. EXPORT_SYMBOL(i2c_smbus_write_quick);
  1063. EXPORT_SYMBOL(i2c_smbus_read_byte);
  1064. EXPORT_SYMBOL(i2c_smbus_write_byte);
  1065. EXPORT_SYMBOL(i2c_smbus_read_byte_data);
  1066. EXPORT_SYMBOL(i2c_smbus_write_byte_data);
  1067. EXPORT_SYMBOL(i2c_smbus_read_word_data);
  1068. EXPORT_SYMBOL(i2c_smbus_write_word_data);
  1069. EXPORT_SYMBOL(i2c_smbus_write_block_data);
  1070. EXPORT_SYMBOL(i2c_smbus_read_i2c_block_data);
  1071. MODULE_AUTHOR("Simon G. Vogl <simon@tk.uni-linz.ac.at>");
  1072. MODULE_DESCRIPTION("I2C-Bus main module");
  1073. MODULE_LICENSE("GPL");