i2c-core.c 32 KB

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